0
0
Angularframework~20 mins

Property binding with square brackets in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Property Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the rendered output of this Angular component?

Consider this Angular component template:

<input [value]="userName" />
<p>Hello, {{ userName }}!</p>

And the component class:

export class AppComponent {
  userName = 'Alice';
}

What will the user see in the input box and the paragraph when the page loads?

Angular
export class AppComponent {
  userName = 'Alice';
}
AInput box is empty and paragraph shows 'Hello, Alice!'
BInput box shows 'userName' and paragraph shows 'Hello, userName!'
CInput box shows 'Alice' and paragraph shows 'Hello, Alice!'
DInput box shows 'Alice' and paragraph is empty
Attempts:
2 left
💡 Hint

Remember that property binding with square brackets sets the property value of the element.

📝 Syntax
intermediate
2:00remaining
Which option correctly binds the disabled property of a button in Angular?

You want to disable a button based on a boolean variable isDisabled in your component. Which of the following is the correct syntax?

A&lt;button [disabled]="isDisabled"&gt;Click&lt;/button&gt;
B&lt;button (disabled)="isDisabled"&gt;Click&lt;/button&gt;
C&lt;button disabled={{ isDisabled }}&gt;Click&lt;/button&gt;
D&lt;button disabled: isDisabled&gt;Click&lt;/button&gt;
Attempts:
2 left
💡 Hint

Property binding uses square brackets around the property name.

🔧 Debug
advanced
2:00remaining
Why does this Angular property binding not update the input value?

Given this template:

<input [value]="userInput" />

And this component code:

export class AppComponent {
  userInput = '';

  ngOnInit() {
    setTimeout(() => {
      this.userInput = 'Hello';
    }, 1000);
  }
}

Why does the input box remain empty after 1 second?

ABecause property binding only sets the initial value and does not update after changes.
BBecause the component does not use two-way binding with [(ngModel)].
CBecause the change detection is not triggered by setTimeout.
DBecause the input element does not support property binding for value.
Attempts:
2 left
💡 Hint

Think about how Angular updates input elements when the model changes.

🧠 Conceptual
advanced
2:00remaining
What is the difference between attribute binding and property binding in Angular?

Consider these two bindings:

<button [attr.aria-label]="label">OK</button>
<button [disabled]="isDisabled">OK</button>

What is the key difference between [attr.aria-label] and [disabled] bindings?

A<code>[attr.aria-label]</code> sets an HTML attribute, while <code>[disabled]</code> sets a DOM property.
B<code>[attr.aria-label]</code> sets a DOM property, while <code>[disabled]</code> sets an HTML attribute.
CBoth set HTML attributes but differ in syntax.
DBoth set DOM properties but differ in syntax.
Attempts:
2 left
💡 Hint

Attributes and properties are different in HTML elements.

state_output
expert
2:00remaining
What is the final value of the button's disabled property after this sequence?

Given this Angular component template:

<button [disabled]="isDisabled" (click)="toggle()">Toggle</button>

And this component code:

export class AppComponent {
  isDisabled = false;

  toggle() {
    this.isDisabled = !this.isDisabled;
    setTimeout(() => {
      this.isDisabled = !this.isDisabled;
    }, 1000);
  }
}

If the user clicks the button once, what will be the disabled state of the button immediately and after 1 second?

AImmediately disabled=false; after 1 second disabled=false
BImmediately disabled=false; after 1 second disabled=true
CImmediately disabled=true; after 1 second disabled=true
DImmediately disabled=true; after 1 second disabled=false
Attempts:
2 left
💡 Hint

Trace the toggle function and the delayed setTimeout reversal.