答案 0 :(得分:1)
ng-value
会将字符串放入option
的值中。转换为字符串的javascript对象为[object Object]
。
但是您可以在m.id
中添加ng-value
以在您的javascript代码中检索它。这是一个例子:
var App = angular.module('myApp', [
'controllers']);
var ctrls = angular.module('controllers', []);
ctrls.controller('myController', ['$scope', function ($scope) {
$scope.moos = [
{'id':1, 'name': 'foo'}, {'id':2, 'name': 'boo'}
];
$scope.proxy = {
setter: function (id) {
if(!arguments.length) return;
var moo = $scope.moos.filter(function(moo) {
return moo.id == id;
})[0];
console.log(moo);
$scope.moo = moo;
}
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.5/angular.js"></script>
<div ng-app="myApp" ng-controller="myController">
<p>{{moo.name}}</p>
<select ng-model="proxy.setter" ng-model-options="{ getterSetter: true }">
<option ng-selected="moo.id===m.id" ng-value="m.id" ng-repeat="m in moos">{{m.name}}</option>
</select>
</div>