I have seen similar questions about this but I still do not get it.
I have a component witch gets list of items Array<{id: number, name: string}> and a list of "checked" items Array<number>
@Component({
selector: 'check-list',
template: `
<div class="form-check" *ngFor="let item of list">
<label class="form-check-label">
<input class="form-check-input"
type="checkbox"
[checked]="checked.includes(item.id)"
(change)="onChange.emit({id: item.id, value: $event.target.checked})">
{{ item.name }}
</label>
</div>
`
})
export class CheckListComponent {
@Input() list: CheckListItem[];
@Input() checked: number[];
@Output() onChange: EventEmitter<any> = new EventEmitter();
constructor() { }
}
If I update the "checked" list the rendered checkbox list gets updated as well, but if I click some of the checkboxes and then updates the "checked" list it does not render as I expected.
Plunker: https://plnkr.co/edit/YSItU48QflE4GZbj58Ev?p=preview
Click on the two buttons in the plunker and it works as I expect, the checkboxes updates according to the "checked" list. But if I click the "item 2" and then click reset, it does not clear "item 2".
I have also tried using [ngModel] and [attr.checked].
The reason I do not want to use [(ngModel)] is that I also have a @Output() which triggers a server request and then if that fails the checkbox should update or not. I'm using ngrx for states.
UPDATE:
So I was not clear enough in my question that I'm using Redux/ngrx and the list sent in to my check-list-component should not be mutated directly by the component itself. I have updated the component to send back outputs on changes
Heres the new plnker: https://plnkr.co/edit/lMouWapI2lb9U6mS9YMy?p=preview
UPDATE 2:
So the problem I have is that click/check will send an update to a server, and if that fails I want the checkbox to be reset to the previous state. I have added a random bool if the server request is a success or not. Maybe I'm using the wrong type of implementation ide for this.
Plunker: https://plnkr.co/edit/nGyS8oYWVzzRULgzcDQV?p=preview