Select in ng-option not updating

I have a list of data coming from the backend, and I want to update the select value in the view, which for some reason does not happen.

I tried ng-selected, which works inefficiently, sometimes the model does not update spmetimes.

here is my code, can anyone help?

    <div class="listitem" ng-repeat="d in data">
        {{d.name}}: 
        <select 
            ng-model="d.option"                 
            ng-options="d.name for d in options"></select>
    </div>

controller

var myApp = angular.module('myApp', []);
myApp.controller("SomeController", function($scope) {
    $scope.options = [{
        "id": "id1",
        "name": "p1"
    }, {
        "id": "id2",
        "name": "p2"
    }];
    $scope.data = [{
        "name": "data1",
        "option": {
            "id": "id1",
            "name": "p1"
        }
    }];
});

http://jsfiddle.net/gFCzV/58/

+4
source share
2 answers

You need to use select as label group by group for value in array track by trackexprread docs

ng-options="option.name for option in options track by option.id"

DEMO , but note that this will not work in Angualrjs 1.1

+3
source

You have the code:

 $scope.options = [{
     "id": "id1",
     "name": "p1"
 }, {
     "id": "id2",
     "name": "p2"
 }];

 $scope.data = [{
     "name": "data1",
     "option": {
         "id": "id1",
         "name": "p1"
     }
 }];

Angular , , $scope.options, , option, $scope.data.

:

  • $scope.data "option": "id1" ng-options d.id as d.name for d in options

  • ng-options "ng-options="d as d.name for d in options" $scope.data, "option": $scope.options[0], , .

1 .

EDIT: JSFIDDLE Angular 1.1, Angular 1.2 @Satpal track by, ( ).

0

All Articles