AngularJS $ add()函数没有触发

时间:2013-03-10 23:22:39

标签: angularjs

我有一个表格,其中填充了表格中的信息。当我的记录具有角色或多个角色时,它会按预期显示,但是我无法添加其他角色,如果没有,我无法将任何角色添加到记录中。我的$ add()函数有问题。

另一个问题是密码和确认密码显示相同的数据。我现在也试图解决这个问题。

<form ng-model="current">
            <fieldset>
                <legend>
                    <a href="" alt="Register" class="btn btn-mini btn-success" ng-click="newItem()"><i
                            class="icon-plus-sign icon-white"></i> New </a>
                    Detail
                </legend>
                <label>First Name</label>
                <input type="text" placeholder="First Name" ng-model="current.firstName" value="current.firstName"
                       ng-disabled="isKey('lastName')">

                <label>Last Name</label>
                <input type="text" placeholder="First Name" ng-model="current.lastName" value="current.lastName"
                       ng-disabled="isKey('firstName')">

                <label>Email</label>
                <input type="text" placeholder="user@example.com" ng-model="current.email" value="current.email"
                       ng-disabled="isKey('email')">

                <label>Password</label>
                <input type="password" placeholder="Password" ng-model="current.password" value="current.Pass"
                       ng-disabled="isKey('Pass')">

                <label>Confirm Password</label>
                <input type="password" placeholder="Confirm Password" ng-model="current.password" value="current.Pass"
                       ng-disabled="isKey('Pass')">

                <label>Enabled</label>
                <select ng-model="current.enabled" ng-disabled="isKey('enabled')">
                    <option value=true>Enabled</option>
                    <option value=false>Disabled</option>
                </select>

                <div class="role" ng-repeat="role in current.roles">
                    <label>User Roles</label>
                    <select ng-model="role.roleName" ng-disabled="isKey('role')">
                        <option value="ROLE_USER">User</option>
                        <option value="ROLE_ADMIN">Administrator</option>
                    </select>
                </div>
                <label></label>
                <a class="btn btn-primary" ng:click="current.role.$add()">Add Role</a>
                <label></label>
                <a href="#askCreateUser" alt="Register" role="button" class="btn btn-success" data-toggle="modal"
                   ng-click="setDataOperation('Update')"><i class="icon-edit icon-white"></i> Update </a>
                <a href="#askCreateUser" alt="Register" role="button" class="btn btn-warning" data-toggle="modal"
                   ng-click="setDataOperation('Delete')"><i class="icon-trash icon-white"></i> Delete </a>
            </fieldset>

        </form>

1 个答案:

答案 0 :(得分:0)

我发现这是因为试图推送到一个空数组。添加以下功能后,我能够毫无问题地添加元素。

 $scope.addRole = function () {
        if ($scope.current.roles != undefined) {
            $scope.current.roles.push({});
        }
        else {
            $scope.current.roles = new Array();
            $scope.current.roles.push({});
        }
    };