AngularJS dropdown not showing selected value

前端 未结 4 1101
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-08 11:06

Am facing problem in displaying selected value in angular dropdown. it works when i give like this

$scope.selectedItem = $scope.items[1];

not w

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-08 11:47

    Well, because

    $scope.items[1] and { name: 'two', age: 27 } is totally different thing.

    { name: 'two', age: 27 } is a totally different object whereas $scope.items[1] is part of the object $scope.items

    When you put something in the template using {{}}, angular add it in its watcher list.

    SO when angular put it in the watch list, it was an object (i.e. { name: 'two', age: 27 } ) that is different than $scope.items.

    selectedItem is attached with the object that you set in the controller. In summary while dirty checking, angular will checks selectedItem against { name: 'two', age: 27 } NOT against $scope.items

    Hope you understand what I mean

提交回复
热议问题