我有下面的选择框
<select class="form-control" ng-model="community_type_id">
<option ng-repeat="x in myData" value="{{ x.community_type_id }}" ng-click="sendID(x.community_type_details)">{{ x.community_type_details }}</option>
</select>
我想将 x.community_type_id 传递给这样的函数 sendID 。但在我的情况下不起作用。
var app = angular.module('EntityApp', []);
app.controller('EntityAppCntroller', function($scope, $http) {
$http.get("http://122.322.5.459:8080/apartment/community/type/list").then(function(response) {
$scope.myData = response.data.type;
});
$scope.sendID = function(id) {
alert(id);
$http.get("http://11.22.33.44:8080/apartment/community/sub/type/list/" + id).then(function(response) {
$scope.myDatas = response.data.type;
});
}
});
最佳答案
您需要更改 html
进行选择,并且必须提供 ng-model
。
那么就不需要传递任何id了。
喜欢:
<select ng-model="Selected"
ng-options="x as x.community_type_details for x in myData"
ng-change="sendID()">
</select>
然后在 Controller 中:
$scope.sendID = function() {
alert($scope.Selected.community_type_details);
alert($scope.Selected.community_type_id);
}
查看工作Fiddle.
关于javascript - 我想将选择框的 ID 传递给 Angular JS 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43248607/