Diego Aaron Diego Aaron - 3 months ago 11
Javascript Question

AngularJS: format of ngClass

I'm learning AngularJS and I see the tag "li" is associated with the ngClass directive which has as "{marked: nav.estoy ( '/')}" values. My question is why the value inside the brackets has the form x: y. I am that "nav.estoy ('/') is defined in the controller method but do not understand why the response of this method is to":"

<body ng-app="app">
<nav ng-controller="navCtrl as nav">
<ul>
<li ng-class="{marcado: nav.estoy('/')}">
<a href="#/">Datos personales</a>
</li>
</ul>
</nav>


...

Answer

ng-class can take an object as an argument: angularjs doc

the keys of the object are the class names to apply, and the values of the object are expressions that will evaluate truthy or falsey. in this case it's saying that the class marcado will be applied if nav.estoy('/') returns true, but it could easily have several keys with several classes to be applied if the values evaluate truthy (non-zero, non-null).

good luck!