在ng-blur中传递下拉选定值

时间:2017-06-15 05:49:01

标签: angularjs

我正在尝试将选定的下拉选项值传递到leaveBalance()方法。
但我得到了undefined如何传递它?

<body ng-app="myApp" ng-controller="myCtrl" ng-init="init()">
                    <div>
                        <label>Leave Type</label>
                        <select type="text" id="levType" ng-blur="leaveBalance(data.id)"><option value="" selected="selected">Select</option><option data-ng-repeat="data in leaveTypes" value="{{data.id}}">{{data.Name}}</option></select>
                    </div>
</body>
<script>
    var app = angular
                    .module("myApp", [])
                    .controller("myCtrl", function ($scope, $http) {
                        $scope.init = function () {
                            $http.post("/user/getList").then(function (response) {
                                $scope.leaveTypes = response.data;
                            })
                        }
                        $scope.leaveBalance = function (id) {
                            alert(id)
                        }
                    })
</script>

1 个答案:

答案 0 :(得分:3)

您应该使用 ng-model 来设置所选值并将其传递给函数

 <select type="text" ng-model="selectedvalue" id="levType" ng-blur="leaveBalance(selectedvalue.id)"><option value="" selected="selected">Select</option><option data-ng-repeat="data in leaveTypes" value="{{data.id}}">{{data.Name}}</option></select>