close

How to add conditional attribute in Angular 2?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to add conditional attribute in Angular 2? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to add conditional attribute in Angular 2 ?

  1. How to add conditional attribute in Angular 2 ?

    to add conditional attribute in Angular 2 If it's an input element you can write something like…. <input type="radio" [checked]="condition"> The value of condition must be true or false.
    Also for style attributes… <h4 [style.color]="'red'">Some text</h4>

  2. How to add conditional attribute in Angular 2 ?

    to add conditional attribute in Angular 2 If it's an input element you can write something like…. <input type="radio" [checked]="condition"> The value of condition must be true or false.
    Also for style attributes… <h4 [style.color]="'red'">Some text</h4>

Method 1

in angular-2 attribute syntax is

<div [attr.role]="myAriaRole">

Binds attribute role to the result of expression myAriaRole.

so can use like

[attr.role]="myAriaRole ? true: null"

Method 2

If it’s an input element you can write something like…. <input type="radio" [checked]="condition"> The value of condition must be true or false.

Also for style attributes… <h4 [style.color]="'red'">Some text</h4>

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment