在angular.js中选择默认选项ng-option

时间:2016-12-27 00:04:00

标签: javascript angularjs

感谢您阅读我的问题。我知道有类似的问题,但我没有解决我的问题,所以我发表了我的问题。我想通过单击某些超链接选项为下拉列表分配值。问题是我永远不会选择任何价值。

<select class="form-control" ng-model='obj.animal' id='animal' name='animal'  
    ng-options="opt as opt.animal for opt in aAnimals  track by opt.animal">
          <option style="display:none" value="">Select an animal</option>
</select>

<div ng-repeat='item in aAnimals'>
 <a href='' ng-click='changeval($index)' >{{item.animal}}</a>
</div>

//********controller********//

$scope.obj = {}
$scope.aAnimals=[{ "animal": "cat", "color": "white"},{ "animal": "dog", "color": "black"}]

$scope.changeval=function(index){
  $scope.obj.animal=$scope.aAnimals[index].animal;
}  

http://plnkr.co/edit/8z51qlqfodGSRA1Rv3UH?p=preview

1 个答案:

答案 0 :(得分:2)

选项是数组中的项目。所以你需要改变这一行:

$scope.obj.animal=$scope.aAnimals[index]; // Remove .animal