我有一个查看对象数组的ngRepeat。
我需要将选定的用户对象传递给我的ngChange函数。
<select
ui-select2
id="entityDropDown"
ng-model="selectedUser"
ng-change="getUserInfo(selectedUser)">
<option></option>
<option ng-repeat="user in users">{{user.name}}</option>
</select>
但是,ngModal只返回user.name而不是整个用户对象。
编辑:我不能在这里使用ngOptions,因为我使用的是不幸与ngOptions不兼容的select2。
答案 0 :(得分:4)
利用ng-value
:
<select
ui-select2
id="entityDropDown"
ng-model="selectedUser"
ng-change="getUserInfo(selectedUser)">
<option></option>
<option ng-repeat="user in users" ng-value="$index">
{{user.name}}
</option>
</select>
然后在你的控制器中:
var app = angular.module("myApp", [])
app.controller("MyCtrl", ['$scope', function($scope) {
$scope.users = [
{name: 'blah blah', something: 'else'}
] ;
$scope.getUserInfo = function(val) {
console.log($scope.users[val]); //logs the object value that you want
}
}]);
演示:http://jsfiddle.net/qwertynl/GENZ2/
或者你可以这样做:
HTML:
<select
ui-select2
id="entityDropDown"
ng-model="selectedUser"
ng-change="getUserInfo(users[selectedUser])">
<option></option>
<option ng-repeat="user in users" ng-value="$index">
{{user.name}}
</option>
</select>
JS:
var app = angular.module("myApp", [])
app.controller("MyCtrl", ['$scope', function($scope) {
$scope.users = [
{name: 'blah blah', something: 'else'}
] ;
$scope.getUserInfo = function(val) {
console.log(val);
}
}]);