How do I prevent a checkbox from looking selected if click handler causes error

ryanmcfall

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?

Milan Tenk

I suggest to put the MouseEvent itself additionally to the toggleCategory as parameter:

<input type="checkbox" (click)="toggleCategory($event, cat)">

And in toggleCategory the $event.target will be the input itself, and its checked porterty can be changed to the wished value. So if we want to prevent the checkbox to be checked, following code snippet can be used:

toggleCategory($event: MouseEvent, category: Category) {
   ($event.target as HTMLInputElement).checked = false;
}

The above example sets the checkbox always back. This code snippet can be in the error cases to prevent the checkbox selection.

Here is a stackblitz example, if you want to play around with it: https://stackblitz.com/edit/angular-op51jp?file=src/app/app.component.ts

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I prevent a batch file from outputting any error?

分類Dev

How do I access the text in a text box from a button click event handler

分類Dev

In Flutter, how do I show a SnackBar from a platform call handler?

分類Dev

How do I entirely remove the label from a CheckBox?

分類Dev

How do I filter a list sorted from Checkbox Values in Angular?

分類Dev

How do I pass an object to a button click event handler in WPF? C#

分類Dev

How do I prevent an object from being extended?

分類Dev

How do I prevent two OSes from clobbering grub files?

分類Dev

How do I render data from selected object?

分類Dev

How do I capture the selected value from a ListBox in PowerShell?

分類Dev

How do I access an object's keys' values? Looking to populate a table from object data

分類Dev

How do I get a click event from a GridViewColumn header?

分類Dev

How do I unregister a Handler in net/http?

分類Dev

How i can add click handler function to dynamically link in Vue?

分類Dev

How do I prevent an Android device from going to sleep from Qt application

分類Dev

PHP: How do I traverse through XML looking for a node?

分類Dev

Looking at the jQuery source, how do I implement this easing function in pseudocode?

分類Dev

How do I get data from a radio/checkbox form submission in the Blazor @code block?

分類Dev

Remove click event handler from <a>

分類Dev

How do I prevent this infinite loop in PowerShell?

分類Dev

How do I get the selected item in MvxLIstView

分類Dev

How do I prevent Dropbox from being automatically added to the autostart list after launching it?

分類Dev

How do I prevent Sublime Text 3 from auto-indenting a line as a one off

分類Dev

How do I prevent Git from auto-detecting user.email?

分類Dev

How do I prevent ASP.NET MVC from binding query parameters?

分類Dev

How do I prevent Windows 8 File Explorer from regularly crashing (about every 10 Minutes)?

分類Dev

Ember.js: How do I prevent this property observer from being hit more than once?

分類Dev

How do I prevent unpinning of items from the Windows 10 Start menu?

分類Dev

How do I prevent wget from loading Apache directory listings in different orders?

Related 関連記事

  1. 1

    How do I prevent a batch file from outputting any error?

  2. 2

    How do I access the text in a text box from a button click event handler

  3. 3

    In Flutter, how do I show a SnackBar from a platform call handler?

  4. 4

    How do I entirely remove the label from a CheckBox?

  5. 5

    How do I filter a list sorted from Checkbox Values in Angular?

  6. 6

    How do I pass an object to a button click event handler in WPF? C#

  7. 7

    How do I prevent an object from being extended?

  8. 8

    How do I prevent two OSes from clobbering grub files?

  9. 9

    How do I render data from selected object?

  10. 10

    How do I capture the selected value from a ListBox in PowerShell?

  11. 11

    How do I access an object's keys' values? Looking to populate a table from object data

  12. 12

    How do I get a click event from a GridViewColumn header?

  13. 13

    How do I unregister a Handler in net/http?

  14. 14

    How i can add click handler function to dynamically link in Vue?

  15. 15

    How do I prevent an Android device from going to sleep from Qt application

  16. 16

    PHP: How do I traverse through XML looking for a node?

  17. 17

    Looking at the jQuery source, how do I implement this easing function in pseudocode?

  18. 18

    How do I get data from a radio/checkbox form submission in the Blazor @code block?

  19. 19

    Remove click event handler from <a>

  20. 20

    How do I prevent this infinite loop in PowerShell?

  21. 21

    How do I get the selected item in MvxLIstView

  22. 22

    How do I prevent Dropbox from being automatically added to the autostart list after launching it?

  23. 23

    How do I prevent Sublime Text 3 from auto-indenting a line as a one off

  24. 24

    How do I prevent Git from auto-detecting user.email?

  25. 25

    How do I prevent ASP.NET MVC from binding query parameters?

  26. 26

    How do I prevent Windows 8 File Explorer from regularly crashing (about every 10 Minutes)?

  27. 27

    Ember.js: How do I prevent this property observer from being hit more than once?

  28. 28

    How do I prevent unpinning of items from the Windows 10 Start menu?

  29. 29

    How do I prevent wget from loading Apache directory listings in different orders?

ホットタグ

アーカイブ