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?
export class AppComponent { userName = 'Alice'; }
Remember that property binding with square brackets sets the property value of the element.
The [value] binding sets the input's value property to the userName string 'Alice'. The interpolation inside the paragraph also shows 'Alice'.
You want to disable a button based on a boolean variable isDisabled in your component. Which of the following is the correct syntax?
Property binding uses square brackets around the property name.
Option A uses property binding syntax [disabled] to bind the boolean isDisabled to the button's disabled property.
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?
Think about how Angular updates input elements when the model changes.
Property binding sets the input's value property initially, but user changes or later updates require two-way binding with [(ngModel)] to keep input and model in sync.
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?
Attributes and properties are different in HTML elements.
Angular's [attr.xxx] syntax sets HTML attributes directly, useful for non-standard attributes. The plain [property] syntax sets DOM properties, which affect element behavior.
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?
Trace the toggle function and the delayed setTimeout reversal.
Click toggles isDisabled from false to true immediately, disabling the button. After 1 second, isDisabled toggles back to false, enabling the button again.