app.component.ts
import { Component, OnInit } from '@angular/core';
import { supportsWebAnimations } from '@angular/animations/browser/src/render/web_animations/web_animations_driver';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
users: Array<any>;
permissions: Array<any> = [];
ngOnInit(){
this.users = [
{id: 1, name: 'Sam', permission: []},
{id: 2, name: 'Adam', permission: []},
{id: 3, name: 'Chris', permission: []}
]
}
}
app.component.html
<h3 class="first">Permission</h3>
<div class="ui-g" style="margin-bottom:10px; ">
<div class="ui-g-2"><b>Name</b></div>
<div class="ui-g-2"><b>Read</b></div>
<div class="ui-g-2"><b>Write</b></div>
</div>
<div class="ui-g" style="margin-bottom:10px;" *ngFor ="let user of users; let i = index">
<div class="ui-g-2">{{user.name}}</div>
<div class="ui-g-2"><p-checkbox name="group[i]" value="Read" [(ngModel)]="permissions[i]" inputId="R"></p-checkbox></div>
<div class="ui-g-2"><p-checkbox name="group[i]" value="Write" [(ngModel)]="permissions[i]" inputId="W"></p-checkbox></div>
</div>
答案 0 :(得分:1)
代替维护permission
数组,您可以维护permission
对象并将其绑定到您的复选框。
在这种情况下,无需维护索引。
您的代码将如下所示:
import { Component, OnInit } from '@angular/core';
import { supportsWebAnimations } from '@angular/animations/browser/src/render/web_animations/web_animations_driver';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
users: Array<any>;
permissions: Array<any> = [];
ngOnInit(){
this.users = [
{id: 1, name: 'Sam', permission: { read: true, write: true}},
{id: 2, name: 'Adam', permission: { read: false, write: true}},
{id: 3, name: 'Chris', permission: { write: true, read: false}}
]
}
}
还有您的HTML:
<h3 class="first">Permission</h3>
<div class="ui-g" style="margin-bottom:10px; ">
<div class="ui-g-2"><b>Name</b></div>
<div class="ui-g-2"><b>Read</b></div>
<div class="ui-g-2"><b>Write</b></div>
</div>
<div class="ui-g" style="margin-bottom:10px;" *ngFor ="let user of users; let i = index">
<div class="ui-g-2">{{user.name}}</div>
<div class="ui-g-2"><p-checkbox type="checkbox" name="group[i]" value="Read" [(ngModel)]="user.permission.read" inputId="R"></p-checkbox></div>
<div class="ui-g-2"><p-checkbox type="checkbox" name="group[i]" value="Write" [(ngModel)]="user.permission.write" inputId="W"></p-checkbox></div>
</div>
并使用 PrimeNG
您需要添加
binary =“ true”
归因于您的复选框
您可以在此处看到demo
答案 1 :(得分:0)
您必须在模型中具有那些读/写标志,并将它们连接到正确的复选框。例如
this.users = [
{id: 1, name: 'Sam', permission: {read:false,write:false}},
{id: 2, name: 'Adam', permission: {read:false,write:false}},
{id: 3, name: 'Chris', permission: {read:false,write:false}}
]
和
<div class="ui-g" style="margin-bottom:10px;" *ngFor ="let user of users; let i = index">
<div class="ui-g-2">{{user.name}}</div>
<div class="ui-g-2"><p-checkbox [(ngModel)]="user.permissions.read" inputId="R"></p-checkbox></div>
<div class="ui-g-2"><p-checkbox [(ngModel)]="user.permissions.write" inputId="W"></p-checkbox></div>
</div>
模型中的值将根据复选框状态相应地切换为true / false。至少这就是我要做的。