Binding Property, Styles And Class In Angular

In this article, we are going to learn how to bind property, class, styles of HTML in Angular.

In this article, we are going to learn how to bind property, class, and styles of HTML in Angular.

Prerequisites

Let us create a sample TestApp. For this, you should have installed the below tools in your development environment:

  1. Node
  2. Npm (comes when you install node)
  3. Angular CLI.
  4. Text Editor.

For creating a new application run the below command at any location.

> ng new TestApp

Create a new component.

> ng g c test

Once your command is completed you will have to TestApp the folder created inside your sample folder.

Now, you will have your project folder called 'TestApp'.

Note
See my previous article “Getting started with Angular CLI” If you want the installation and introduction from the basics and start with the execution of a sample application.

Property binding

  • HTML attribute vs Dom property
    Attribute and property are not the same. The attribute is defined by HTML while the property is defined by DOM. Attribute initializes the DOM property and cannot change once they are initialized. Property can be changed.

Eg

Open test.component.html

Add the below code.

  1. <h1>Property Binding in angular!</h1>  
  2. <input type="text" value="Mohammad Irshad">  

Run the application. Inspect the element input text box.

Binding Property, Styles And Class In Angular 

$0 represents the current element and the attribute value is set to the given value.

Let us change the input textbox value and inspect and check the value.

Binding Property, Styles And Class In Angular 

You can see the attribute value is the same as it was initialized but the property value is changed anytime.

Let us add a property value inside the class and use that ID in your HTML.

Open test.component.ts and add the below property.

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   public testId = "test";  
  10.   constructor() { }  
  11.   ngOnInit() {  
  12.   }  
  13. }  

Open test.component.html and add the below content:

  1. <h1>Property Binding in angular!</h1>  
  2. <input type="text" value="Irshad" id={{testId}}>  

Run and inspect the element,

Binding Property, Styles And Class In Angular 

You can see the ID set the same as given value in test.component.ts

Better way to bind the property is to enclose the id inside [] bracket and assign that ID to it.

  1. <h1>Property Binding in angular!</h1>  
  2. <input type="text" value="Irshad" [id]="testId">  

When you inspect this element you will find that this is set as an ID attribute with ‘test’ ID assign to it. This is how we do property binding.

Now, we are going to do the same thing with the help of interpolation.

  1. <input id=”{{testId}}” type=”text” value=”Irshad”/> 

Inspect the same browser you will find the same result.

There are certain limitations of Interpolation.

  • It can only work with string values. Sometimes the situation arises when we need to deal with the Boolean value in the property. Eg: disabled property.

When we define disabled property it automatically initializes it to false.

If we define it like,

  1. <input disabled type=”text”/>  
  2. <input disabled=”false” type=”text”/>  
  3. <input disabled=”{{false}}” type=”text”/>  

Interpolation doesn’t work on Boolean value. So it will not affect on giving it a value.

If we define it like,

  1. <input [disabled]=”false” type=”text”/>  
  2. <input [disabled]=”true” type=”text”/>  

Then it will be working according to its Boolean value and enable/disable in browser.

Open test.component.ts and add the below content,

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public testId = "test";  
  11.   public disableControl = false;  
  12.   constructor() { }  
  13.   ngOnInit() {  
  14.   }  
  15. }  

Inside test.component.html add the content below-

  1. <h1>Property Binding in angular!</h1>  
  2. <input disabled={{disableControl}} type="text" value="Irshad" [id]="testId"
Binding Property, Styles And Class In Angular 

The textbox should not be disabled, as we have defined value of ‘disableControl’ to false just because interpolation is not working with Boolean value. So to make it work add the below content.

  1. <h1>Property Binding in angular!</h1>  
  2. <input [disabled]="disableControl" type="text" value="Irshad" [id]="testId"
Binding Property, Styles And Class In Angular 

This time the textbox is not disabled and works with the value assigned to it.

There is an alternative way to bind the property to the element.

You can use ‘bind-‘ prefix with the property name to bind it. We don’t need to use brackets.

Eg

  1. <input type=”text” bind-disabled=”disabled” />  

This is all about property binding in Angular.

Class Binding

Let us create a CSS class as below.

Open test.component.css and add the below content.

  1. .red{  
  2.     color:red;  
  3. }  
  4. .green{  
  5.     color:green;  
  6. }  
  7. .blue{  
  8.     color:blue;  
  9. }  
  10. .yellow{  
  11.     color:yellow;  
  12. }  

The most common way to apply a class is to use ‘class’ attribute.

Open test.component.html and add the below content.

  1. <h1>Class Binding in angular!</h1>  
  2. <p class="red">This is p tag with class - red.</p>  
  3. <p class="green">This is p tag with class - green.</p>  
  4. <p class="blue">This is p tag with class - blue.</p>  
  5. <p class="yellow">This is p tag with class - yellow.</p>  

In order to bind a class to your HTML from property defined in component class. Declare property in your component class.

Open test.component.ts and add below content.

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public classRed = "red";  
  11.   public classGreen = "green";  
  12.   public classBlue = "blue";  
  13.   public classYellow = "yellow";  
  14.   constructor() { }  
  15.   ngOnInit() {  
  16.   }  
  17. }  

Open test.component.html and add the below content,

  1. <h1>Class Binding in angular!</h1>  
  2. <p class="{{classRed}}">This is p tag with class - red.</p>  
  3. <p class="{{classGreen}}">This is p tag with class - green.</p>  
  4. <p class="{{classBlue}}">This is p tag with class - blue.</p>  
  5. <p class="{{classYellow}}">This is p tag with class - yellow.</p>  

It is better to use [] brackets,

  1. <h1>Class Binding in angular!</h1>  
  2. <p [class]="classRed">This is p tag with class - red.</p>  
  3. <p [class]="classGreen">This is p tag with class - green.</p>  
  4. <p [class]="classBlue">This is p tag with class - blue.</p>  
  5. <p [class]="classYellow">This is p tag with class - yellow.</p>  

Both will return the same output,

Binding Property, Styles And Class In Angular

Define class on the basis of condition.

Open test.component.ts and add contents below,

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   public setRed = true;  
  10.   constructor() { }  
  11.   ngOnInit() {  
  12.   }  
  13. }  

Open test.component.html and add below content.

  1. <h1>Class Binding in angular!</h1>  
  2. <p [class.red]="setRed">This is p tag with color red based on condition.</p> 

Now, this class will be applied depending on the value true/false of set Red property.

Binding Property, Styles And Class In Angular

ngClass Directive

This directive is used to apply multiple class to HTML tag element.

Open test.component.css and add the classes below,

  1. .red{  
  2.     color:red;  
  3. }  
  4. .bgYellow{  
  5.     background-color: yellow;  
  6. }  
  7. .italic{  
  8.     font-style: italic;  
  9. }  

Open test.component.ts and add below content,

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public classObj = {  
  11.     red : true,  
  12.     bgYellow : true,  
  13.     italic: true  
  14.   };    
  15.   constructor() { }  
  16.   ngOnInit() {  
  17.   }  
  18. }  

Open test.comonent.html and add below content.

  1. <h1>Class Binding in angular!</h1>  
  2. <p [ngClass]="classObj">This is p tag with color red based on condition.</p>  

Run the application.

Binding Property, Styles And Class In Angular 

In a classObj object, the ngClass will check which classes are true and need to be applied. It is very useful to add the class dynamically and remove the class whenever not required in HTML based on user requirements. So, in your component class, you will need to specify the properties to apply bases on which the template will be utilizing the class values.

Style Binding

Let us try binding an inline style to the HTML element.

  1. <p [style.color]=”’orange’”> P tag with inline style </p>  

We can also bind the inline style on the basis of the condition.

  1. <p [style.color]=”hasColor ? ‘orange’ : ‘green’”> P tag with inline style based on condition </p>  

Inside component class declare the ‘hasColor’ property.

  1. Public hasColor = true;  

We can also add style object which contains multiple styles.

Eg

Open test.component.ts and add the following code,
  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public styleObj = {  
  11.     "color" : "blue",  
  12.     "background-color" : "yellow",  
  13.     "font-style""italic"  
  14.   };  
  15.   constructor() { }  
  16.   ngOnInit() {  
  17.   }  
  18. }  

Open test.component.html and add the following code,

  1. <h1>Class Binding in angular!</h1>  
  2. <p [ngStyle]="styleObj">This is p tag with color red based on condition.</p>  

Run the application.

Binding Property, Styles And Class In Angular