我有一个 ng-repeat,我把它放在一个 select 标签中。 我希望能够修改不同选择的选定值。 这是我所拥有的: HTML
<div ng-controller="Ctrl">
<div ng-repeat="user in users">
<select ng-model="nbModel" ng-options="numValue.Text for numValue in numValues" ng-change="chooseUser(nbModel, user.id)">
</select>
{{user.name}} - {{user.age}} years
</div>
</div>
js
function Ctrl($scope) {
$scope.numValues = [ {Id : '0',Text : '0'}, {Id : '1',Text : '1'}, {Id : '2',Text : '2' }];
$scope.users = [];
$scope.users.push(new User(1, 'Luke', 23));
$scope.users.push(new User(2, 'Maria', 26));
$scope.users.push(new User(3, 'Mike', 45));
angular.forEach($scope.users, function(equip) {
//TODO to set the default values for each select
});
$scope.chooseUser= function(value, name) {
//some traitement
//TODO to set a wanted value
};
};
function User(id, name, age) {
this.id=id;
this.firstnamename = name;
this.age = age;
}
我不知道如何手动设置选定的选项。 有帮助吗?
最佳答案
您已经完成了一半...您需要做的是将 ng-model 设置为您想要更改的用户。假设您想通过选择更改用户的年龄,它看起来像这样:
<div ng-controller="Ctrl">
<div ng-repeat="user in users">
<select ng-model="user.age" ng-options="numValue.Text for numValue in numValues"></select>
{{user.name}} - {{user.age}} years
</div>
</div>
关于javascript - 如何在 ng-repeat 中设置 ng-model 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15008823/