使用ng-option的下拉人口

时间:2017-09-10 13:39:00

标签: angularjs

我有html:

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Current: {{currentApplication}}</a>
                    <ul class="dropdown-menu" >
                        <li ng-repeat="application in applications" ng-click="changeApplication(application.name)" >
                            <a ui-sref="home">{{application.name}}</a>
                        </li>
                    </ul>
                </li>

我希望将application.id作为值,将application.name作为显示值。我该怎么办?

1 个答案:

答案 0 :(得分:1)

只需将application.id传递给ng-change上的函数,

 <select ng-model="selected" ng-change="changeApplication(application.id)" ng-options="application.name for application in applications">

<强>样本

&#13;
&#13;
var app = angular.module('testApp',[]);
app.controller('MyCtrl',function($scope){
    $scope.applications =[{
  "id": 1,
  "name": "Alexandre"
}, {
  "id": 2,
  "name": "Arvy"
}, {
  "id": 3,
  "name": "Vidovic"
}]
  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="testApp" ng-controller="MyCtrl">
  <select ng-model="selected" ng-change="changeApplication(application.id)" ng-options="application.name for application in applications">
  </select>
</div>
 
&#13;
&#13;
&#13;