即使模型已更改,Angular 4复选框状态也不会更新

时间:2017-10-26 07:22:47

标签: angular typescript checkbox angular2-forms

我知道这个问题仍然存在,但根据此解决方法Angular 2 - Checkbox not kept in sync它应该按预期工作,但我仍然很难使其适用于我的案例。

我有一个授予角色的权限列表。当用户想要更新角色时,我会将包含可编辑角色名称的表单及其权限列表显示为复选框列表。现在,如果角色已经拥有列表中的权限,我需要检查一些列表项。

我的表格:

<tr *ngFor ="let perm of permissions, let i = index">
        <td>
            <label>
            <input type="checkbox" 
                    name="check-box-{{perm.permission_alias}}"                                            
                    value="{{perm.permission_id}}"  
                    (ngModel)="perm.checked"
                    (ngModelChange)="onSelectFilter($event,perm)"
                    attr.id="check-box-{{perm.permission_alias}}"
                    /> 
            {{perm.permission_alias}}
            </label>                                
        </td>
    </tr>

我的组件:

getPermissions(role: Role): void {
    this.permissions = [];
    this.userService.getPermissions().then(perms => {            
        if (role != null) {
            this.permissions = perms;
            for (let rp of role.permissions) {          
                let index = this.permissions.indexOf(this.permissions.find(p => p.permission_id == rp.permission_id));
                if (index >= 0) {  
                     this.permissions[index].checked = true;
                    //this.onSelectFilter(true, this.permissions[index]); not sure if this should be called
                }      
            }
            console.log("before selected perms on update");
            console.log(this.permissions);
        }
        else {
            this.permissions = perms;
        }

    }).catch(this.handleError);
}

   onSelectFilter(selected: boolean, filter: Permission) {
        filter.checked = selected;
        if (this.permissions.every(filter => !filter.checked)) {
            setTimeout(() => {
                this.permissions.forEach(filter => filter.checked = true);
            });
        }
    }

我不知道为什么这不起作用,我没有在列表中检查过任何项目。 我很感激任何帮助。

3 个答案:

答案 0 :(得分:0)

我认为这里的错误是因为你总是'改变数组'。当角度变化检测运行时,它看不到任何变化,并且不更新ngFor。试试这段代码,看看我唯一要做的就是在变异之前复制数组。

getPermissions(role: Role): void {
this.permissions = [];
this.userService.getPermissions().then(perms => {            
    if (role != null) {
        this.permissions = perms;
        for (let rp of role.permissions) {          
            let index = this.permissions.indexOf(this.permissions.find(p => p.permission_id == rp.permission_id));
            if (index >= 0) {  
                 this.permissions = this.permissions.slice(0) // copy the array.
                 this.permissions[index].checked = true;
                //this.onSelectFilter(true, this.permissions[index]); not sure if this should be called
            }      
        }
        console.log("before selected perms on update");
        console.log(this.permissions);
    }
    else {
        this.permissions = perms;
    }

}).catch(this.handleError);
}

   onSelectFilter(selected: boolean, filter: Permission) {
    filter.checked = selected;
    if (this.permissions.every(filter => !filter.checked)) {
        setTimeout(() => {
            this.permissions = this.permissions.slice(0) // copy the array.
            this.permissions.forEach(filter => filter.checked = true);
        });
    }
}

答案 1 :(得分:0)

这很有效。它将始终使用双向数据绑定保持同步。

<input type="checkbox" 
name="check-box-{{filter.text}}"
[(ngModel)]="filter.selected"
attr.id="check-box-{{filter.text}}">

如果您需要在更改时触发某些功能,请使用更改事件。

<input type="checkbox" 
name="check-box-{{filter.text}}"
[(ngModel)]="filter.selected"'
(change)="myCustomFun()"
attr.id="check-box-{{filter.text}}">

通过解决方法链接

plunk中更新了相同内容

答案 2 :(得分:0)

请使用[(ngModel)]和布尔变量,因为复选框是两种方式的数据绑定