I am trying to display checkboxes for my user roles:
For example, I have two user roles: 1. Admin 2. Employee
I have an array of roles in the userObject:
user={
"name":"Bhushan",
"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e7edf0f6ede4ebc5fce4edeaabe6eae8">[email protected]</a>",
"roles":['Admin','Employee']
}
I want to use a reactive form to populate this model into a form. I want to populate the roles array into read-only checkboxes i.e. when the form loads, the user can edit the name & email but checkboxes will show 'admin' toggled if the user has an admin role or untoggled if he is not an admin. The same can be said for the employee role.
So far I have tried the following:
<form [formGroup]="userForm" (ngSubmit)="onSubmit()" novalidate>
<div style="margin-bottom: 1em">
<button type="submit" [disabled]="userForm.pristine" class="btn btn-success">Save</button>
<button type="reset" (click)="revert()" [disabled]="userForm.pristine" class="btn btn-danger">Revert</button>
</div>
<div class="form-group">
<label class="center-block">Name:
<input class="form-control" formControlName="name">
</label>
</div>
<div class="form-group">
<label class="center-block">Email:
<input class="form-control" formControlName="email">
</label>
</div>
<div class="form-group" *ngFor="let role of roles;let i=index">
<label>
// I tried this, but it doesn't work
<!--<input type="checkbox" [name]="role" [(ngModel)]="role">-->
{{role}}
</label>
</div>
</form>
<p>userForm value: {{ userForm.value | json}}</p>
Any suggestions?