简体   繁体   English

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

[英]I want to pass ID of the select box to the function in Angular JS

I have the below select box 我有以下选择框

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

I want to pass x.community_type_id to a function sendID like this. 我想将x.community_type_id传递给这样的函数sendID。 But not working in my case. 但在我的情况下不起作用。

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;
        });
    }
});

You need to change your html for select, and has to provide ng-model . 您需要更改select的html ,并且必须提供ng-model Then there is no need to pass any id. 然后,无需传递任何ID。

Like : 喜欢 :

 <select ng-model="Selected" 
  ng-options="x as x.community_type_details for x in myData" 
  ng-change="sendID()">
</select>

Then in controller : 然后在控制器中:

$scope.sendID = function() {
alert($scope.Selected.community_type_details);
alert($scope.Selected.community_type_id);
}

See working Fiddle. 参见工作小提琴。

 angular.module('app', []).controller('MyController', ['$scope', function($scope) { $scope.myData = [ {community_type_id:1, community_type_details:'details1'}, {community_type_id:2, community_type_details:'details2'}, {community_type_id:3, community_type_details:'details3'}, ]; $scope.sendID = function(){ $scope.id = $scope.myData.find(function(x){ return x.community_type_id == $scope.community_type_id; }).community_type_details; console.log('sendID with ' + $scope.id + ' called'); } }]); 
 <script src="//code.angularjs.org/snapshot/angular.min.js"></script> <body ng-app="app"> <div ng-controller="MyController"> <select class="form-control" ng-model="community_type_id" ng-change="sendID()"> <option ng-repeat="x in myData" value="{{ x.community_type_id }}">{{ x.community_type_details }}</option> </select> <p>community_type_id: {{community_type_id}}</p> <p>id: {{id}}</p> </div> </body> 

 var myApp = angular.module("myApp", []); myApp.controller("myController", function($scope){ $scope.list= [ { id:1,name:'Ajay'}, { id:2,name:'jay'}, { id:3,name:'ay'}, { id:4,name:'y'}, { id:1,name:'Ajay'}]; $scope.hasChanged = function(selectedID){ alert(selectedID) } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="myApp"> <div ng-controller= "myController"> <select ng-options="item.name for item in list" ng-model="selected" ng-change="hasChanged(selected.id)"> </select> </div> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM