Why is this Angular expression in an ng-class inside an ng-repeat always treated as true?

Jean-Philippe Pellet

I can't get my head around this.

I am displaying a collection of objects with ng-repeat, like this:

<div ng-repeat="obj in objs">{{obj | json}}</div>

Then, I want to apply the class current to one of these <div> elements based on whether the object it represented is the “current object”; i.e., in practice, whether obj points to the same thing as another scope variable current.

I tried this:

<div ng-repeat="obj in objs" ng-class="{current: 'obj == current'}">{{obj | json}}, current: {{obj == current}}</div>

The funny thing is: the expression obj == current is correctly evaluated in the body of the div, and is initially false for all of them, when the value of current in the scope is null. But the class is applied to all of them nevertheless, although it is supposed to depend on the exact same expression.

Whenever I change the value of current to point to another object, the text inside the div updates accordingly, and changes to true, but the class always stays applied.

What am I missing here? Why is the class applied even if the expression yields false?

To play with it: JSFiddle.

Kurt Funai

The quotes are causing this to fail. It should be changed to:

ng-class="{'current': obj == current}"

Edited your JS Fiddle: http://jsfiddle.net/qXgJm/1/

The quotes were causing it to always return true, because the string was truthy

Example: This would evaluate to true, and log the '!!!!'

if ("hello world") {
  console.log('!!!!');
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why is angular ng-repeat adding properties to my model?

분류에서Dev

angular ng-repeat with condition

분류에서Dev

angular filter for the ng-repeat

분류에서Dev

input tag inside ng-repeat angularjs

분류에서Dev

How do I pass a function to an angular directive inside ng-repeat (arg is undefined)?

분류에서Dev

AngularJs ng-repeat in group class

분류에서Dev

Angular ng-options object linked to ng-repeat object

분류에서Dev

angular.js ng-repeat with arrays

분류에서Dev

Angular ng-repeat with routeParams and nested array

분류에서Dev

angular directive ng-repeat scope and Service

분류에서Dev

Angular - Data Returned But ng-repeat Not Displaying It

분류에서Dev

Angular ng-repeat animate once

분류에서Dev

Angular Ng-Repeat First Of Type

분류에서Dev

How to split the div structure in ng repeat using ng-class-even and ng-class-odd?

분류에서Dev

filter function ng-repeat returning true but 0 results are displayed

분류에서Dev

How to manipulate many elements inside an ng-repeat?

분류에서Dev

Jquery Time Picker inside ng-repeat is not working

분류에서Dev

Angular js ng-class false 조건이 ng-repeat에서 작동하지 않습니다.

분류에서Dev

ng-class expression: merge condition and class name

분류에서Dev

Using OR operator within Angular ng-class

분류에서Dev

Why isn't my ng-repeat working?

분류에서Dev

Learning to angular: ng-view not loading, why?

분류에서Dev

Angular JS table with ng-repeat and radio buttons

분류에서Dev

Skip ng-repeat JSON ordering in Angular JS

분류에서Dev

Angular의 ng-repeat 요소 간 간격

분류에서Dev

angular $index numbering with condition in ng-repeat & ui-sortable

분류에서Dev

ng-repeat Angular의 증가 된 숫자

분류에서Dev

Angular ng-repeat binding to array of objects in array

분류에서Dev

Angular Directive needs access to variable in ng-repeat

Related 관련 기사

  1. 1

    Why is angular ng-repeat adding properties to my model?

  2. 2

    angular ng-repeat with condition

  3. 3

    angular filter for the ng-repeat

  4. 4

    input tag inside ng-repeat angularjs

  5. 5

    How do I pass a function to an angular directive inside ng-repeat (arg is undefined)?

  6. 6

    AngularJs ng-repeat in group class

  7. 7

    Angular ng-options object linked to ng-repeat object

  8. 8

    angular.js ng-repeat with arrays

  9. 9

    Angular ng-repeat with routeParams and nested array

  10. 10

    angular directive ng-repeat scope and Service

  11. 11

    Angular - Data Returned But ng-repeat Not Displaying It

  12. 12

    Angular ng-repeat animate once

  13. 13

    Angular Ng-Repeat First Of Type

  14. 14

    How to split the div structure in ng repeat using ng-class-even and ng-class-odd?

  15. 15

    filter function ng-repeat returning true but 0 results are displayed

  16. 16

    How to manipulate many elements inside an ng-repeat?

  17. 17

    Jquery Time Picker inside ng-repeat is not working

  18. 18

    Angular js ng-class false 조건이 ng-repeat에서 작동하지 않습니다.

  19. 19

    ng-class expression: merge condition and class name

  20. 20

    Using OR operator within Angular ng-class

  21. 21

    Why isn't my ng-repeat working?

  22. 22

    Learning to angular: ng-view not loading, why?

  23. 23

    Angular JS table with ng-repeat and radio buttons

  24. 24

    Skip ng-repeat JSON ordering in Angular JS

  25. 25

    Angular의 ng-repeat 요소 간 간격

  26. 26

    angular $index numbering with condition in ng-repeat & ui-sortable

  27. 27

    ng-repeat Angular의 증가 된 숫자

  28. 28

    Angular ng-repeat binding to array of objects in array

  29. 29

    Angular Directive needs access to variable in ng-repeat

뜨겁다태그

보관