Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set class in angular if variable is defined?

Tags:

angularjs

I'm trying to set the class "expanded" to a div if the variable is undefined. I attempted this, but had no success. What am I doing wrong?

{'expanded': typeof member != 'undefined'}
like image 489
Himmators Avatar asked Dec 02 '13 09:12

Himmators


People also ask

Can we use class and ngClass together?

You can use both class and ngClass as the first one gives you the opportunity to apply a class that you want to implement in all cases under any circumstances and the later to apply classes conditionally.

What is the difference between class and ngClass?

ngClass is more powerful. It allows you to bind a string of classes, an array of strings, or an object like in your example. attr. foo is for binding to attributes instead of properties.

What does ngClass do in Angular?

ngClass is a directive in Angular that adds and removes CSS classes on an HTML element.


1 Answers

You can do it easier:

 ng-class="{'expanded':  member != undefined}"

or even:

ng-class="{'expanded':  member }"

Example: http://plnkr.co/edit/9RMulfYViGKf91743VZz?p=preview

like image 81
Ivan Chernykh Avatar answered Oct 10 '22 09:10

Ivan Chernykh