InterviewSolution
| 1. |
Explain property binding in Angular? |
|
Answer» Property binding in Angular is used to bind HTML PROPERTIES to VARIABLES and objects. For example, the image url (the src property) of the <img> HTML element can be bound to a string property. This allows us to bind values to properties of an element to modify their behavior or appearance. The SYNTAX for property bindings is to PUT the property onto the element wrapped in brackets [property]. The name should match the property, usually in a camel case like src. Here is an example of the TS and HTML code. TS Code imageUrl: string = "https://via.placeholder.com/150"/>HTML Code <img [src]="imageUrl" height="150" width="150"/>In the above code, the imageUrl is a TS property (of type String) that holds the URL to an image resources. Since the src property of the image HTML element is bound to the imageUrl property using the property binding syntax, the value of the imageUrl property will be evaluated first and then assigned to the src property. If you change the value of the imageUrl property, the image element will reflect the changes in REAL time. |
|