How To Use 2d Array On Ngmodel In Angular 2?
Would like to know how to set 2d array in ngmodel for dynamic checkbox? I have a role and permission setup form for a super admin with multiple checkbox. How to differentiate mod
Solution 1:
As ngModel is directive to obtain 2-way binding system.
You can easily do the syntax:
[(ngModel)]="permission[role.id][per.id]"
where permission is an empty array defined initially as:
permission = []
or
permission = [[]]
where your permission variable will be a multidimensional array.
After this, if you try to ngModel
[(ngModel)]="permission[role.id][per.id]"
it will cause a undefined issue as ngModel not only gets the value from input it has to show also that is how are 2-way binding works.
To avoid issue while rendering due to undefined, we have to assign dummy data to the permission Array initially only.
this.permission = newArray(this.numberOfRoles).fill(0);
for (let j = 0; j < this.numberOfRoles; j++) {
this.permission[j] = newArray(this.numberOfPermission).fill(0);
}
This solves the undefined issue such as below
Post a Comment for "How To Use 2d Array On Ngmodel In Angular 2?"