0

Actually, i'm trying to bind the array of object to select input. I'm trying in this way, [(ngModel)]="users[i].roles", where 'users' is outer array and 'roles' is another array inside users. Here, roles is array of key value pairs like,

roles = [{'code': 1, 'desc: ''},{'code': 2, 'desc: ''},{'code': 3, 'desc: ''}]

How can i bind the 'code' from this array.

My HTML code,

 <mat-select placeholder="User Roles" [(ngModel)]="users[i].roles" matTooltip="{{users[i].roles}}" multiple>
    <mat-option *ngFor="let role of userRoles" [value]="role.code">{{role.desc}}</mat-option>
 </mat-select>
1
  • Do you have *ngFor to loop through users? Could you provide code Commented Sep 12, 2018 at 5:50

3 Answers 3

2

try to use *ngFor inside a <div> element

<div *ngFor="let role of roles">
    <input type="number" [(ngModel)]="role.code">
</div>

for more details look here.. How properly bind an array with ngModel in angular 4?

Sign up to request clarification or add additional context in comments.

1 Comment

You have no need use index in template. [(ngModel)]="role.code"
0

put it in an ngFor :

in .ts :

roles= [{'code': 1, 'desc: ''},{'code': 2, 'desc: ''},{'code': 3, 'desc: ''}]

in .html

<div *ngFor="let role of roles;let index=index">
    <input [(ngModel)]="roles[index].code">
</div>

Comments

0

thanks for your help. I solved this problem by modifying code in this way...

for (let user of this.users) {
   user.userRoles = [];
   for(let role of user.roles)
   {
     user.userRoles.push(role.code);
   }
}

In html,

<mat-select placeholder="User Roles" [(ngModel)]="users[i].userRoles" matTooltip="{{users[i].userRoles}}" multiple>
    <mat-option *ngFor="let role of userRoles" [value]="role.code">{{role.desc}}</mat-option>
</mat-select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.