Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在ng repeat中设置ng模型的值?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在ng repeat中设置ng模型的值?

Javascript 如何在ng repeat中设置ng模型的值?,javascript,angularjs,Javascript,Angularjs,我有一个ng重复,我把它放在一个select标记中。 我希望能够修改不同选择上的选定值。 以下是我所拥有的: html 我不知道如何手动设置所选选项。 有什么帮助吗?你已经走到一半了。。。您需要做的是将ng model设置为您想要更改的用户上的某些内容。假设您希望使用select更改用户的年龄,它将如下所示: <div ng-controller="Ctrl"> <div ng-repeat="user in users"> <select

我有一个ng重复,我把它放在一个select标记中。 我希望能够修改不同选择上的选定值。 以下是我所拥有的: html

我不知道如何手动设置所选选项。
有什么帮助吗?

你已经走到一半了。。。您需要做的是将ng model设置为您想要更改的用户上的某些内容。假设您希望使用select更改用户的年龄,它将如下所示:

<div ng-controller="Ctrl">
    <div ng-repeat="user in users">
        <select ng-model="user.age" ng-options="numValue.Text for numValue in numValues"></select>
        &nbsp;&nbsp;{{user.name}}&nbsp;&nbsp;-&nbsp;{{user.age}} years
    </div>
</div>

{{user.name}-{{user.age}}年

谢谢,我明白了,ng模型可以是动态的吗?我的意思是像ng model=“userModel[user.id]”这样的东西?如果是,如何从js$scope.userModel[1]=?是的,如果您愿意,ng模型可以是动态的。
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;
}
<div ng-controller="Ctrl">
    <div ng-repeat="user in users">
        <select ng-model="user.age" ng-options="numValue.Text for numValue in numValues"></select>
        &nbsp;&nbsp;{{user.name}}&nbsp;&nbsp;-&nbsp;{{user.age}} years
    </div>
</div>