Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use one way binding with ngClass directive in AngularJS?

Tags:

angularjs

I have a list of objects. They are displayed with ngRepeat direcitve. Each block with information is covered with directive, that has some helper methods.

i.e. it constructs labelId variable, that is used in this block with one way binding.

So when I want to display it for example in for attribute I have no problem {{::label}}.
When I want to display it as field name, there is also no problem {{::label}}.

But when I want to reference it in ngMessages block like:

<p class="error-message" ng-messages="FormName[::label + '_' + $index].$error" ng-messages-include="error-messages.html"></p>

Or when I want to use it for reference to set css class dynamically like:

<div ng-class="{'has-error': (FormName[::label + '_' + $index].$error.someErrorName)}">

then I'm getting Error

Error: [$parse:syntax] Syntax Error: Token ':' not a primary expression at column 21 of the expression [{'has-error': (FormName[::label + '_' + $index].$error
like image 772
Eugene Avatar asked Sep 11 '15 10:09

Eugene


1 Answers

ng-class has one way binding syntax too

ng-class="::{'has-error': ..., 'no-error': ...}"

But it is one expression even if there is many classes inside.

So if you need two-way binding for some classes inside that ng-class, then one way binding cannot be used.

like image 125
YOU Avatar answered Sep 16 '22 17:09

YOU