0

我有以下DIV:

<div ng-controller="userControl">
    <div ng-repeat="user in users">
        <u>{{user.id}} : {{user.name}}</u><br />
        <div ng-repeat="role in user.roles">
            {{role.name}}
        </div>
        <br />
    </div>
    <form ng-submit="addRoleToUser()">
        <select name="userSelect">
        <div ng-repeat="user in users">
                <option value="{{user.id}}">{{user.name}}</option>
        </div>
        </select>
        <input type="submit" value="add">
    </form>
</div>

这是我的控制器:

function userControl($scope,$http) {
    $scope.users = [
                        {"id":"0","name":"Username1","roles":[{}]},
                        {"id":"1","name":"Username2","roles":[{}]},
                    ]

    $scope.addUser = function() {
        var nextid = $scope.getNextId();
        $scope.users.push({id:nextid,name:$scope.userName});
        /*
        $.post({}); //Push changes to server
        */
        $scope.userName = '';
    };

    $scope.getNextId = function() {
        var count = 0;
        angular.forEach($scope.users, function(data) {
          count = data.id;
        });
        var count2 = parseInt(count)+1;
        return count2;
     };

}

我可以看到第一个 ng-repeat 工作正常并且列出了user.idand user.name,但是第二个 ng-repeat (表单内的那个)没有显示信息,这与我已经循环过的事实有关前一个 div 中的元素?

4

1 回答 1

1

您滥用了 select 指令:

<select ng-model="selectedUser" name="userSelect" ng-options="user.id as user.name for user in users">

http://docs.angularjs.org/api/ng.directive:select

于 2013-05-27T11:03:55.877 回答