I have multiple checked checkbox which comes in(ngOninit).now i edit this checked checkboxes and sometimes i without edit i save checkboxes that time it makes this.checklist empty but there where some checkbox is checked so when i not edit checkboxes that time how to put default checked checkboxes in this.checkedlist or any other solution is there is appreciated.
<label *ngFor="let statusCategoryObj of statusCategoryObj">
<mat-checkbox value="{{statusCategoryObj.categorytitle}}" name="categories"
[checked]="checkedCategories(statusCategoryObj.id)" (change)="onCheckboxChange(statusCategoryObj,$event)">
{{statusCategoryObj.categorytitle}}</mat-checkbox>
</label>
<button mat-raised-button (click)="updateStatusDetailsById({'id':this.editStatusObj.id})">SAVE</button>
edit-status.component.ts
ngOnInit(){
this.statusService.editStatusDetailsById({'id': id}).subscribe(
(data) => {
if(data.status == 28){
this.editStatusObj.id = data.payload[0].id;
this.editStatusObj.categories = data.payload[0].categories;
this.allCategories = this.editStatusObj.categories.split(',');
}
}
)
}
checkedCategories(id){
for(var i = 0 ; i < this.allCategories.length; i++){
if(id == this.allCategories[i]){
return true;
}
}
return false;
}
onCheckboxChange(statusCategoryObj, event) {
if(event.checked) {
this.checkedList.push(statusCategoryObj.id);
}else{
for(var i=0 ; i < this.statusCategoryObj.length; i++) {
if(this.checkedList[i] == statusCategoryObj.id){
this.checkedList.splice(i,1);
}
}
}
}
updateStatusDetailsById(id){
const formData = new FormData();
formData.append('categories',this.checkedList.toString());
this.statusService.updateStatusDetailsById(formData).subscribe(
(data) => {
if(data.status == 29){
console.log(data.payload);
}
}
)
}