FREELessons: 67Length: 8.9 hours

• Overview
• Transcript

# 4.6 The NgClass and NgStyle Directives

The NgClass and NgStyle directives are very similar to the class and style bindings, but they can set multiple classes or styles simultaneously. In this lesson we'll focus on these useful directives.

## 10.Conclusion1 lesson, 01:32

### 4.6 The NgClass and NgStyle Directives

Hi folks. In this lesson, we're going to take a look at the NgClass and NgStyle directives, which are similar in intent to the class and style bindings, except that with these directives, we aren't restricted to setting a single class name or a single style. We can set many at the same time. Like Angular 1.X, modern versions of Angular also include directives. And in fact components are just specialized directives. Directives function in the same way that they did before. They are used to alter the behavior or appearance of elements or components. Let's take a look at the NgClass directive first. It's on of Angular's built-in directives, of which there are many, and it's very commonly used. So, in the start component, let's say that we want to show a couple of different states for the name input and label, like an error state or a warning state depending on the values entered into the input. We won't add proper validation. We do that in the form section later in the course, we'll just simulate it for now. We use square brackets around the directive NgClass and the expression points to an object in the component class called states. The object doesn't exist yet, so let's add it to the component. The type of property is going to be an object, so let's initialize the property now in the Ng, on in-it life cycle method. So let's just hardcode the values for now. We've got two properties warning and error. Warning is set to true and error is set to false. So the property is an object, and each key in this object is a class name. If the value for that class name is true, the class name will be added to the element that the directive is applied to. So, let's add some SESS for these states to the forms.sess, SESS file. So, inside the label we can add this SESS. And then inside the input we can add some more. The ampersand at the end targets the parent of the current selector, which is where we've added the directive. So let's go back to the browser now. And we find that the label and the input now have the orange styling. And we can see that the parent row has the warning class. So let's go back and just set the error state to true as well. And we'll go back to the browser, and we'll see that the styling is now red, and the parent row has both the classes, warning and error. We won't be using the Ng style directive in the example app, so let's just revert things back. The NgStyle directive works in a very similar way to the NgClass directive, except that in the template we use NgStyle instead of NgClass. In the objects back in the component class, we use the names of style properties as keys instead of class names. So now we can see that the style property has set the color to red. We don't want to use NgStyle in the example app however, so let's just revert that back to NgClass. The NgClass and NgStyle directives are known as attribute directives because they are added to the elements as attributes. They are distinguished by square brackets. Other types of directives might use a different syntax. So in this lesson we learned how to use the NgClass and NgStyle directives, which are both very commonly used. The reason for using these directives instead of the Class or Style bindings is that with directives we can set multiple class names or styles at once. With the bindings that we looked at in the last lesson, we can only set a single class or style. Thanks for watching.

Back to the top