I want to prevent a checkbox from becoming selected if a handler bound to the click event encounters an error. Here's a section of my component's HTML
<input class="form-check-input" type="checkbox"
id="category-{{cat.id}}" value="{{cat.id}}"
(click)="toggleCategory(cat)"
[checked]="menuItem.hasCategory(cat.id)"
>
https://github.com/angular/angular/issues/2042 indicates that returning false from the toggleCategory
method should work, and indeed it does: clicking on the checkbox does not cause the checkbox to appear checked when toggleCategory
looks like this:
toggleCategory(category:Category):boolean {
return false;
}
However, the actual implementation of toggleCategory
invokes an HttpClient
POST, and so it's not known until later whether an error occurred.
I tried making toggleCategory
return Observable<boolean>
but that doesn't work.
toggleCategory(category:Category):Observable<boolean> {
return of(false);
}
I've seen Angular 2 Checkbox preventDefault in which the accepted answer seems to suggest wrapping the <input> element in an <a> and handling the click event there. This behaves the same way as clicking on the checkbox does for me.
Any ideas?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…