Attribute directives change the appearance or behavior of DOM elements or components. It is used just like a normal HTML attribute. However, the directive should be enclosed within square brackets [ ] to bind it to the element.
Built-in Attribute Directives
The most commonly used attribute directives are as follows −
ngClass: It adds or removes CSS classes in HTML elements.
ngModel: This directive is used for two-way binding.
NOTE: To use ngStyle and ngClass directive, importing CommonModule is necessary as they are part of this module. For ngModel, we are required to import FormsModule.
The ngStyle Directive
The ngStyle directive is used to add dynamic styles. You can set one or more style properties using key-value pairs separated by a colon. Here, key is the name of style and the value is an expression that gets calculated. If the result is null, no style will be applied to the element.
Example
The example below shows how to apply blue color to the paragraph using ngStyle directive.
Step 1: Create a directive-app application using the below command:
ng newdirective-app
Step 2: Add the below content in app.component.html file.
<p [ngStyle]="{'color': 'blue', 'font-size': '14px'}">
paragraph style is applied using ngStyle
</p><router-outlet />
Step 4: Start your application (if not done already) using the below command −
ng serve
Now, run your application and you can see the below response −
The ngClass Directive
The ngClass directive is used to add or remove CSS classes in HTML elements. The CSS classes are updated based on given conditions or type of expression.
If the expression is a string, the CSS classes specified in the string are added. If it’s an array, the CSS classes listed as array elements are added. And, if the expression is an object, the keys represent CSS classes that will be added when their corresponding values are evaluated to a truthy value and removed when their values are falsy.
Example
Let’s try ngClass directive in our directive-app application.
Step 1: Change the above code of the app.component.html file with the following code:
Step 4: Finally, start your application (if not done already) using the below command −
ng serve
Now, run your application and you can see the below response −
The ngModel Directive
The ngModel is a built-in attribute directive. It is mostly used to enable two-way data binding for HTML form elements with the help of banana-in-a-box syntax "[()]". However, you if you use ngModel in square bracket [] syntax, it will enable the one-way binding.
The ngModel directive is provided by a separate module called FormsModule, which is necessary for template-driven forms.
Example
A simple example of two way binding is given below. Here, a user object with name property is defined. Whenever user change the input field, the updated user name will be reflected to the user.name variable in the component. Similarly, if the components variable, user.name gets changed, the input fields value will be updated as well.
Let us create a simple registration form to understand ngModel directive. Our registration form will have three input fields as shown below and a button to submit the registration form:
Username
Password
Confirm password
Step 1: Create a new application, my-app using angular CLI as shown below −
ng newmy-app
Step 2: Create a new registration form component, RegisterForm using angular CLI as shown below −
ng generate component RegisterForm
Step 3: Open the registration form component's template and a user with username, password and confirm password.
Here, ngModel sets the value from "user" object to HTML input element. It sets the user-entered value to the user object in the reverse direction as well.
Step 4: Open the registration form component's style and add some CSS to the form as shown below −
Step 6: Next, add an object named "user" with username, password and confirmPassword properties. The "user" object will have an empty string initially. Once the user enters the data, it will get populated through NgModel directive.
user: any ={
username:'',
password:'',
confirmPassword:''}
Step 7: Create a new member method, showInfo() in the component class. The purpose of the method is to collect the current user information and show it through alert() method.
showInfo(e: Event){
e.preventDefault();let info: string ='';
info +='Username = '+this.user.username;
info +='\nPassword = '+this.user.password;
info +='\nConfirm password = '+this.user.confirmPassword;alert(info)}
Here,
preventDefault() method of the event object will prevent the default action of submit button.
alert() method will show the message to the user.
Step 8: The complete listing of the component i.e. register-form.component.ts is as follows:
Step 10: Now, run the application, fill out the form and click the register button. It will collect the information through ngModel binding and show it through the alert() function.
Leave a Reply