- Overview
- Transcript
4.2 Property Bindings
In this lesson we'll see how to bind a property of a component class to a property of an element or component. You can use this type of binding to control the value of many common properties like the element image source, href, classes, and more.
1.Introduction6 lessons, 42:00
1.1Introduction00:48
1.2Get Started With Angular-CLI11:09
1.3Developing With Angular-CLI13:17
1.4TypeScript vs. JavaScript06:54
1.5Angular Modules From the CLI04:31
1.6CLI Options05:21
2.Get Started With Angular7 lessons, 42:38
2.1Bootstrapping the Application04:30
2.2The Application Module04:15
2.3The Application Component08:06
2.4Component Styling03:06
2.5Global Styling05:11
2.6Creating a Component With the CLI09:34
2.7Creating a Service With the CLI07:56
3.Core Concepts7 lessons, 55:20
3.1Component Trees06:20
3.2Dependency Injection06:52
3.3Content Projection05:38
3.4Component and Directive Lifecycle Methods06:31
3.5Component-Only Lifecycle Methods05:28
3.6Decorators07:36
3.7Models16:55
4.Template Deep Dive11 lessons, 1:10:56
4.1Basic Data Binding With Interpolation05:35
4.2Property Bindings07:07
4.3Attribute Bindings03:29
4.4Event Bindings08:16
4.5Class and Style Bindings05:44
4.6The `NgClass` and `NgStyle` Directives05:04
4.7The `*ngIf` Directive04:41
4.8The `*ngFor` Directive09:29
4.9Inputs05:33
4.10Using Pipes in a Template07:31
4.11Using Pipes in a Class08:27
5.Forms10 lessons, 1:45:41
5.1Handling User Input With Template Reference Variables07:06
5.2Template-Driven Forms11:10
5.3Template-Driven Forms: Validation and Submission14:00
5.4Reactive Forms11:26
5.5Using a `FormBuilder`08:01
5.6Reactive Validation With Built-in Validators14:53
5.7Creating Custom Validators for Template-Driven Forms12:18
5.8Creating Custom Validators for Reactive Forms08:26
5.9Observing Form State Changes12:40
5.10Working With the `@HostListener` Decorator05:41
6.Routing9 lessons, 1:15:10
6.1Defining and Configuring Routes07:53
6.2Rendering Components With Router Outlets10:14
6.3Using Router Links for Navigation05:25
6.4Navigating Routes Using the Router06:24
6.5Determining the Active Route Using an Activated Route07:16
6.6Working With Route Parameters10:42
6.7Using Route Guards07:36
6.8Observing Router Events10:55
6.9Adding Child Routes08:45
7.Using the HTTP Client5 lessons, 56:24
7.1Sending an HTTP Request10:52
7.2Handling an HTTP Response11:22
7.3Setting Request Headers12:33
7.4Intercepting Requests09:04
7.5Finishing the Example Application12:33
8.Testing10 lessons, 1:23:27
8.1Service Unit Test Preparation10:45
8.2Unit Testing Services13:24
8.3Component Unit Test Preparation12:35
8.4Unit Testing Components07:27
8.5Unit Testing Component Templates06:58
8.6Unit Testing Pipes04:41
8.7Unit Testing Directives04:56
8.8Unit Testing Validators04:48
8.9Unit Testing Observables11:37
8.10Unit Testing HTTP Interceptors06:16
9.Building for Production1 lesson, 03:40
9.1Building for Production03:40
10.Conclusion1 lesson, 01:32
10.1Conclusion01:32
4.2 Property Bindings
Hi folks, in this lesson we're going to take a look at how we can bind to the properties of HTML elements in our templates. Property bindings are similar to simple interpolation in that they represent a one way transfer of data. Property bindings can only set an element or component's properties, we can't use them to read data from an element. So let's see some property binding in action. A common use case is disabling or enabling a button based on the value of some property in the component class. In the start component, we do have a button. So let's add a property binding for the button's disabled property. We use square brackets around the name of the property that we want to bind to. And in this case we can specify a property of the component, and that is called disable button. The component doesn't currently have this property, so let's go and add it. So we specify it as a public property. Remember that if you want to use a property of your component class in the template, it must be public. And we set the type to be Boolean. So let's just go back to the browser quickly. So because the property doesn't evaluate to true, the button is not yet disabled. But now in the ngOnInit, let's just set that to true. And let's go back to the browser now. And we can see that because the property does evaluate to true, the button is now disabled. If the value in the component changes at all, that change will be reflected in the UI. So we've used a settimeout here, which will change the value of the disable button property to false after one second. So now after one second the button becomes enabled again. We aren't restricted to Boolean values either, we combined two string values and use a more complex expression in the template. So let's delete that settimeout that we just added. And let's change the property to be a string value instead of a Boolean. And now we'll need to update where we set the value in the ngOnInit, which is just giving me a LIM warning. So now we can test for the value of the string in the template. So now we're saying, if the value of the disable button is equal to the string yes, then the button should be disabled. So if we go back to the browser at this point, we find that the button is disabled. So this works just as well but just as with interpolation, it's best to keep the logic in the component class where it can be tested instead of in the template here. In this case it's not too bad, but when we start to get lots of and or or conditions, it can become very hard to read. Let's put it back to how it was cuz I think that was actually preferable. Another common use for a property binding is binding to an element's hidden property, which is used to show or hide the component. We can use this with elements and we can also use it with other components as well. So let's say that we want to hide the game and end components to begin with. We can add a property binding for these in the home component. And now we just need to add the properties to the home component class. So by default in type script, a property that doesn't have an access modifier, like this one, title, will automatically be public. But I prefer to explicitly state whether properties are public or private. And I just think it makes the class more readable, especially when you've got lots of properties. And some of them are public and some of them are private or there might be a few protected ones in there as well. If you've just got some random ones here that don't have any access modifier at all, then it just makes it less readable, personally I find that. You might not think the same, but I'm just gonna add the public keyword before this existing types of property. And now we can add a couple more properties for the property bindings that we've just added in the template. So let's save that and now let's go back to the browser. And we find that the entire game and end components have been completely hidden. Even though they're not actually html elements, we can still use the hidden property binding with them. And just to reiterate, we use square brackets around the property that we would like to bind to. In this case, that's the hidden property that's on the left-hand side of the expression. And on the right-hand side of the expression, following the equals sign, is the property that we are binding to. So one point to note is that the square bracket syntax that we've used in this lesson is only for element properties, not element attributes. Attributes and properties are conceptually similar, and sometimes elements can have both a property and an attribute with the same name. And whose values are kept in sync automatically by the browser. The difference between them is that HTML defines attributes while the dom defines properties. Angular has a separate syntax for attribute binding which we look at in another lesson. So in this lesson we've learned about property binding, which allows us to bind properties of our components or the elements within our components to properties in our classes. We saw that we use square brackets around the property that we want to bind to, followed by an expression within double quotes. Which Angular will evaluate and update the binding with the appropriate value. Thanks for watching.