Bird
0
0

You want to toggle a boolean property isVisible in your component when a button is clicked. Which template statement correctly does this?

hard📝 Application Q9 of 15
Angular - Templates and Data Binding
You want to toggle a boolean property isVisible in your component when a button is clicked. Which template statement correctly does this?
A<button (click)="isVisible ? false : true">Toggle</button>
B<button (click)="toggleIsVisible()">Toggle</button>
C<button (click)="isVisible = !isVisible">Toggle</button>
D<button (click)="isVisible == !isVisible">Toggle</button>
Step-by-Step Solution
Solution:
  1. Step 1: Understand toggling boolean in template

    You can directly assign the negation of isVisible to itself in the template statement.
  2. Step 2: Evaluate each option

    <button (click)=\"isVisible = !isVisible\">Toggle</button> correctly toggles isVisible by assigning !isVisible. <button (click)=\"toggleIsVisible()\">Toggle</button> requires a method, the ternary option is an expression but not an assignment, and the comparison option uses == instead of assignment.
  3. Final Answer:

    <button (click)=\"isVisible = !isVisible\">Toggle</button> -> Option C
  4. Quick Check:

    Use assignment with !property to toggle booleans [OK]
Quick Trick: Toggle booleans with isVisible = !isVisible in template [OK]
Common Mistakes:
  • Using comparison (==) instead of assignment (=)
  • Trying to toggle without assignment
  • Assuming method is required for toggling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes