Diego Aaron Diego Aaron -4 years ago 180
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">
<li ng-class="{marcado: nav.estoy('/')}">
<a href="#/">Datos personales</a>


Answer Source

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download