Explore key concepts and scenarios involving data binding in Angular through easy, focused questions. This quiz covers essential details to solidify your understanding of how binding works and is applied in Angular applications.
Which approach best represents one-way data binding in Angular for displaying variable values?
Explanation: {{ variable }} is used for one-way binding from component to template, outputting the variable value. [variable] is not a correct Angular syntax, [(variable)] is for two-way binding, and { variable } is not a valid Angular binding syntax.
What is the correct syntax for two-way data binding in Angular?
Explanation: [(ngModel)] uses both square and round brackets to enable two-way binding, keeping model and view in sync. [ngModel] binds data one way, (ngModel) listens for an event, and {ngModel} is not Angular syntax.
How do you bind an HTML element property to a component property in Angular?
Explanation: [property]='value' is the correct way to bind a property in Angular. (property)='value' listens for events, {{property:value}} is not valid syntax, and [[property]]='value' is not used in Angular.
Which syntax should be used to bind a DOM event, such as a click, to a method in Angular?
Explanation: (event)='method()' binds an event like (click) to a method in the component. {{ event:method() }} and [[event]]='method()' are not valid. [event]='method()' is for property, not event, binding.
For forms, which directive allows Angular to track and update input values using two-way binding?
Explanation: ngModel enables two-way binding for input elements. ngForm is for grouping controls, ngInput is not a directive in Angular, and ngBind is not the standard way to achieve two-way binding in Angular.
What is the main purpose of using string interpolation with double curly braces in Angular templates?
Explanation: String interpolation {{ ... }} is used to display component data inside HTML. It cannot trigger events, does not directly style elements, and is unrelated to importing scripts.
Which of the following sets an <img> element's source dynamically in Angular?
Explanation: [src]='imagePath' enables property binding for the image source. (src)='imagePath' would treat src as an event, {{src:imagePath}} is not valid syntax, and [[src]] is not used by Angular.
How can you call a function when a button is clicked in an Angular template?
Explanation: (click)='onClick()' binds the click event to the function. [click]='onClick()' is property binding and does not handle events; {click:onClick()} is invalid; (button)='onClick()' is not recognized by Angular.
Which is a main difference between one-way and two-way data binding in Angular?
Explanation: One-way data binding moves data from the component to the template, while two-way binding keeps data synchronized both ways. Event triggering and input support are not accurate, and stating they are identical is incorrect.
What does using both square and round brackets together (e.g., [(property)]) indicate in Angular?
Explanation: [(property)] syntax enables two-way binding. It is not limited to event binding, does not involve static assignments, and is not only for one-way data transfer.