Currently, I am working on an Angular form that involves the dynamic nature of the userEntitiesRoles array. To ensure smooth functionality, each mat-select tag within the ngFor loop requires a unique name attribute. In order to achieve this, I attempted to generate random strings using the TypeScript code provided below. However, upon testing, I encountered an error in Chrome's console as outlined at the end of this post. Any insights or suggestions on how to resolve this issue?
html:
<div *ngFor="let item of userEntitiesRoles">
<mat-form-field>
<mat-label>{{'USERS.ROLE' | translate}}</mat-label>
<mat-select name="role-{{generateRandomString(5)}}" [(ngModel)]="item.role" required>
<mat-option [value]="optionRole" *ngFor="let optionRole of roleOptions">{{optionRole | uppercase}}</mat-option>
</mat-select>
</mat-form-field>
</div>
Typescript:
generateRandomString(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
error:
ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'zAx5R'. Current value: 'rPFo6'..