1

good day .. I have this code and I need to send the role..but when I select the checkbox the ngModel changes to True or False and it is what is sent to the DB..How can I solve it?

<mdl-checkbox class="mdl-textfield--full-width" *ngFor="let role of roles; let i = index"
[(ngModel)]="user.roles[i] " name="ckb{{i}}">
{{role.name}}
</mdl-checkbox>
AT82
  • 71,416
  • 24
  • 140
  • 167

3 Answers3

0

Hard to say with only template. But it might be that you are overwriting the data if you are sending user, since you are using [(ngModel)]="user.roles[i]" on your checkbox, you are altering that value by clicking on the checkbox. You can create an item on the roles array by doing role.isChecked. Then you can store the checked state.

Krister
  • 1
  • 1
0

I would suggest altering your object.

public class Role {
    id: number;
    name: string;
    enabled: boolean;
}

<mdl-checkbox class="mdl-textfield--full-width" *ngFor="let role of roles; let i = index" [(ngModel)]="user.roles[i].Enabled" name="ckb{{i}}">
    {{role.name}}
</mdl-checkbox>
Daniel
  • 533
  • 4
  • 13
  • I have some problems I think it's because 'roles' is an object inside a model.ts called users import {Role} from '../roles/role.model'; export class User { id: string; username: string; password: string; name: string; lastNameF: string; lastNameM: string; phone: string; officePhone: string; roles: Role[]; } – Mauricio Soto Oct 25 '17 at 19:09
  • When you post your model, put your Enabled Roles on the User. – Daniel Oct 25 '17 at 19:29
0

May be you can set user roles in change event? Like (change)="userRoles[i] = role" A sample plunkr using Angular only:

https://plnkr.co/edit/erIQNgk6u1JxOc6zCoHW?p=preview

Keerthi Kumar P
  • 1,554
  • 9
  • 14