1. 程式人生 > >Angularjs 關於Angularjs的ng-options設定下拉框的value的值

Angularjs 關於Angularjs的ng-options設定下拉框的value的值

<div ng-app="myApp" ng-controller="myCtrl">
    <select ng-model="selectedName" ng-options="item.id as item.name for item in names">
//item.id這個部分是設定value,item.name這個部分是設定label的屬性值,names為下方的names,這個類似foreach
        <option value="">123</option>
    </select>
    <input value="123" type="button" ng-click="ff()">
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.names = [{name: "aaaa", id: "1"},
            {name: "bbbb", id: "2"},
            {name: "ccc", id: "3"}];
        $scope.ff = function () {
            //取值
            alert($scope.selectedName);
        }
    });
</script>

請看以上程式碼