By Gary simon - Apr 01, 2017
Property binding simply means you're passing data from the component class and setting the value of a given element in the view. Property binding is one way, in that the data is transferred from the component to the class.
The benefit of property binding is that it allows you to control element property values from the component and change them whenever needed.
Let's take a closer look at property binding based on the previous lesson from the course.
Be sure to subscribe to the Coursetro Youtube Channel for more awesome dev videos!
There are 3 ways to define a property binding in Angular:
<img src="{{ angularLogo }}">
<img [src]="angularLogo">
<img bind-src="angularLogo">
To make this work, we just have to define angularLogo in the component property:
export class AppComponent {
angularLogo = 'https://angular.io/resources/images/logos/angular2/angular.png';
}
After saving and running ng serve on the command line, you'll see that the view shows 3 logos, as all 3 img elements work.
Another common use-case for property binding is on an input element's disabled attribute. It allows you to control when a given input element should be enabled or disabled.
Add the following to the template:
<button [disabled]="buttonStatus">My Button</button>
And add the buttonStatus property in the component class:
buttonStatus = true;
Because buttonStatus is true, the disabled attribute is applied to the button. If you change it to false, it becomes clickable.
You can also define a different template expression:
<button [disabled]="buttonStatus == 'enabled'">My Button</button>
And change the buttonStatus property to:
buttonStatus = 'enabled';
We'll see this works too, as the button is disabled.
This, in a nutshell, is how property binding works. To recap: