I have a dynamic list of data, and each element is shown as a checkbox. The elements I checked are saved in an array. After I save the array, I want all the checked checkbox to be unchecked when I press a button, but it doesn't work.
I want to unchecked the selected checkbox after I saved the array. Please help me to find a solution. Thanks.
ts file:
export class AppComponent {
userRoleListTemp: any = [];
userRoleListToSave: any = [];
checkedInfo: any;
appUserRoleList: any = [
{id: '1', roleName: 'SETUP_ROLE'},
{id: '2', roleName: 'ENTRY_ROLE'},
{id: '3', roleName: 'SEATPLAN_ROLE'},
{id: '4', roleName: 'MARKSENTRY_ROLE'},
{id: '5', roleName: 'APPLICANT_ROLE'}
];
constructor() {}
onChangeRole(userRole: string, isChecked) {
this.checkedInfo = isChecked;
if (isChecked.target.checked) {
this.userRoleListTemp.push(userRole);
} else {
let index = this.userRoleListTemp.indexOf(userRole);
this.userRoleListTemp.splice(index, 1);
}
}
checkedEvnt() {
this.checkedInfo.target.checked = false;
}
}
html file:
<h1>Unchek All Roles</h1>
<div class="form-check" *ngFor="let appUserRole of appUserRoleList">
<input class="form-check-input" name="{{appUserRole.roleName}}"
type="checkbox" id="{{appUserRole.roleName}}"
(change)="onChangeRole(appUserRole.roleName, $event)">
<label class="form-check-label" for="{{appUserRole.roleName}}">
{{appUserRole.roleName}}
</label>
</div>
<button (change)="checkedEvnt()">Uncheck All</button>
<pre>{{ userRoleListTemp | json}}</pre>
checkedproperty. Or use template variable for your checkboxes and access them in your .ts file. I believe both options have been mentioned in the answers. Just choose the one you're comfortable with!