- Overview
- Transcript
4.5 Class and Style Bindings
The class and style bindings are commonly used to manage a single class name or inline style on an element. They use a similar syntax to property bindings, making them intuitive and easy to use. We'll see how to use both types in this lesson.
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.5 Class and Style Bindings
Hi folks. In this lesson, we are going to look at the style and class bindings. We can use these bindings to add and remove class names, or inline styles on elements. These are like specialized property bindings for working exclusively with class and style properties. And Angular provides these, because these are such common requirements. Let's take a look at class bindings first. In the template for the start component, there's a custom check box. It doesn't currently work, but we can fix it up with some help from the class binding. So we have this span here for our custom check box. And what we want to do is link the checked property to a value in the component class. So we still use square bracket syntax for the class binding, and as I said, that's because the class binding is a specialized property binding. So this time we prefixed the class name that we would like to add with the word class. So the binding becomes class.checked, because we want to add a class called checked, and we link that to a property in our component called Aces High. So whenever this property evaluates to true, this element will get the checked class. And when the property evaluates to false, the class will be removed. And we're also going to want to bind to the click event. So let's add that, at this point, as well. So let's open up the component class now. So we don't have either an aces high property, or a toggle aces method. So we're gonna need to add both of those. So all the toggle aces method does is flip the value of the aces high property. So if this property starts out as true, calling this method will set it to false. But if the property is already false, calling the method will set the property to true. So we haven't initialized the aces high property, and so its value will initially be undefined, which evaluates to false, so aces will not be high by default. So let's go back to the browser now. And we should find that we should be able to click the check box, and it will add the checked class. And it looks like it's working, let's just take a look at the elements. So we can see it currently has the checked class, and that class gets removed. But it doesn't affect any other classes that the element might have, it already has this check box class, and that remains whether or not the checked class is present. So the class binding is useful for adding or removing a single class name. There is also another form that the binding can take, instead of toggling a single class, we can overwrite the whole class property. So this time it breaks everything, because it starts out with a class name undefined, and that is not what we want. So I just wanted to point out that this is available, but it works in a slightly different way. So if you really want to set one class, then using the class.class name format is a safer option. We can still only add a single class if we use just class by itself. It just overrides other classes. So you'll find that this version is the most common. The style binding works in a very similar way to the class binding. Let's say we wanted to set the color of one of the labels in the start component as an end line style. So, we still use square brackets for the style binding. And this time we use style., and then the name of the style that we want to set, which is this case is color. Liz had a method here called get color. And in this case, the method that we've just bound to will need to return the value that gets used as the color. So this binding can only set a single style property at a time, just like the class binding. One point to note is that when setting style properties that are usually hyphenated, is that we can use either a hyphen or camelcase. So, if we wanted to set the background color instead of the color, we could do either this. Or we could do this. There's no difference, it's really down to personal preference. So we don't actually want to do that for the label, I just wanted to show you the format of the binding. As you can see, it's very similar to other property bindings that we've used. But let's get rid of that. So in this lesson we looked at the class and style bindings. We saw that either of them can only set a single class name or a single inline style. But that the syntax used by the binding is almost identical to a regular property binding. We just specify either class.className or style.styleProperty. Thanks for watching.