javascript - 我想将选择框的 ID 传递给 Angular JS 中的函数

标签 javascript angularjs

我有下面的选择框

<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/

相关文章:

javascript - angular.js 两个指令,第二个不执行

javascript - Date.getTime() 为相同的时间戳给出两个不同的值

javascript - Rails - Turbolinks - 回调

javascript - Angular-carousel 无法在 safari 浏览器中工作

angularjs - 如何在angularjs中切换按钮状态?

javascript - AngularJS 无法工作

javascript - 现在如何在 Controller 内格式化 $scope.date ?

javascript - 在 AngularJS 中将对象文字保存到 localStorage

javascript - jQuery - 如何使用 getjson 调用回调

javascript - 单元测试注入(inject)依赖关系,如 angularJS 中的服务 Controller