利用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);
}
}]);
演示:http://jsfiddle.net/qwertynl/fBJYP/
使用'NG-options'這裏將實現你想要什麼。 –
是的,這是我以前使用的,它工作完美,但後來我將select2引入到我的應用程序中,不幸的是,它與ngOptions不兼容。你需要設置選項值,如'
{{user.name}}' – Satpal