Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-option inside ng-repeat

Very simple problem, but not sure how to solve it

I have ng-repeat, that iterate model video.

the model has a chosen value and I want to see it in the dropdown list:

<div data-ng-repeat="singleVideo in model.videos">
    {{singleVideo}}<select data-ng-model="singleVideo.name" ng-options="item.name for item in videoList"></select>
</div>

this is the video model:

$scope.model = {
                 videos:[
                    {id:1,name:"VIDEO_ONE"},
                    {id:2,name:"VIDEO_TWO"}
                ]
            }

this is the videoList item:

$scope.videoList = [
                {id:1,name:"VIDEO_ONE"},
                {id:2,name:"VIDEO_TWO"},
                {id:3,name:"VIDEO_Three"}
            ];

simply I expect to see that the first dropdown value will be set to VIDEO_ONE the second dropdown value will be set to VIDEO_TWO.

Currently the dropdown is empty.

How can I achieve that?

This is the expected result, currently i'm getting the dropdown boxes empty

like image 614
Liad Livnat Avatar asked Jan 13 '15 13:01

Liad Livnat


People also ask

What is difference between ng-repeat and Ng-options?

ng-options is the directive which is designed specifically to populate the items of a dropdown list. One major advantage using ng-options for the dropdown is, it allows us to pass the selected value to be an object. Whereas, using ng-repeat the selected value can only be string.

How do you put filters on NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

What can I use instead of NG-repeat?

But ng-repeat is not the right thing to use when you have large datasets as it involves heavy DOM manipulations. And you should consider using ng-repeat with pagination. You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

Does ng-repeat create a new scope?

Each iteration of ng-repeat creates a new child scope, and that new child scope always gets a new property.


2 Answers

You just need to set a value for the ng-options with item.id as item.name for item in videoList. see code below.

(i set the id as the value, since i think it's better suited. somewhere you also need to update your name attribute according to id. or vice verca. your choice.)

angular.module('testapp', [])
.controller('appCtrl', function($scope) {
  $scope.model = {
    videos:[
      {id:1,name:"VIDEO_ONE"},
      {id:2,name:"VIDEO_TWO"}
    ]
  };
  
  $scope.videoList = [
    {id:1,name:"VIDEO_ONE"},
    {id:2,name:"VIDEO_TWO"},
    {id:3,name:"VIDEO_Three"}
  ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="testapp">
  <div ng-controller="appCtrl">
    <div ng-repeat="singleVideo in model.videos">
      {{singleVideo.name}}
      <select ng-model="singleVideo.id" ng-options="item.id as item.name for item in videoList" ng-selected="true"></select>
    </div>
  </div>
</body>
like image 188
Simon Wicki Avatar answered Sep 27 '22 20:09

Simon Wicki


<div data-ng-repeat="singleVideo in model.videos">
    {{singleVideo}}<select data-ng-model="singleVideo.name" ng-options="item.name as item.name for item in videoList"></select>
</div>
like image 35
Rishi Avatar answered Sep 27 '22 20:09

Rishi