Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Styling of an Angular component

Tags:

angular

I am trying to style a component on basis of a condition in following way.

<div class="col-md-3 d-flex justify-content-center align-items-center" ng-style ="{ ' background-color' : (vars.state=='Signup')? '#73c7af' : '#ffffff' }"> 

and my vars.state=Signup. So it according to this background of this div should be #73c7af but it still white. Can anybody tell me where I am making mistake?

like image 928
Shoaib Iqbal Avatar asked Aug 03 '17 12:08

Shoaib Iqbal


People also ask

How we can add style to the particular component?

There are several ways to add styles to a component: By setting styles or styleUrls metadata. Inline in the template HTML. With CSS imports.

What is conditional styling in react?

What Is Conditional Styling? In simple words, it is changing CSS based on a set of conditions or a state. The CSS that will be changed and/or added based on a condition can be inline styles or styled components among the many different ways of adding CSS in React.


2 Answers

ng-style is the syntax for AngularJS. For Angular 2+, use [ngStyle]="" syntax. (see documentation)

<div class="col-md-3 d-flex justify-content-center align-items-center"       [ngStyle]="{'background-color': (vars.state=='Signup') ? '#73c7af' : '#ffffff'}"> 
like image 167
JSON Derulo Avatar answered Sep 28 '22 00:09

JSON Derulo


You can use style.propertyName to set any conditional style property.

<div class="col-md-3 d-flex justify-content-center align-items-center" [style.background]="someFunction()"> 
like image 41
Nour Avatar answered Sep 28 '22 02:09

Nour