0

I have ng-repeat for <li> element:

<li ng-repeat="item in filtered =(categorySelect | filter:searchinput)" ng-class="categorySelect[$index]checked"

I tried to get attribute checked from object categorySelect like as:

categorySelect[$index]checked

But it does not work.

Babaev
  • 101
  • 10
  • 1
    That's not a valid syntax. What do you want to achieve? Do you realize that the index will depend on the filter? You're using the index of an element in a filtered array to access the non-filtered array. Why don't you use `item.checked`? – JB Nizet Aug 15 '15 at 15:55

1 Answers1

1

Because your JS syntax is invalid. Try one of these:

categorySelect[$index].checked

or

categorySelect[$index]['checked']

or preferably avoid $index altogether:

item.checked

Also you're using ng-class incorrectly, it works like this:

ng-class="{myClass: item.checked}"

This will add "myClass" if the checked property evaluates to true, and remove the class if it does not.

Also note that using "dot notation" with reserved keywords like "checked" can break older browsers, like IE9. If that occurs, and supporting IE is necessary, then try to use the array syntax I gave in the second example or use a different property name than "checked".

Josh Ribakoff
  • 2,948
  • 3
  • 27
  • 26
  • How is "checked" a reserved JavaScript keyword? It's not in Microsoft's own list: https://msdn.microsoft.com/library/0779sbks%28v=vs.94%29.aspx – JB Nizet Aug 15 '15 at 16:08
  • Its not technically but IE got the spec wrong years ago, and treats various built-ins as reserved keywords, even when used in valid contexts. See for example - http://stackoverflow.com/a/8814616/2279347 I do not know for a fact if "checked" causes this problem, but it was worth mentioning. OP stated "it does not work" without giving any error message or stating in what browser(s), so for all we know he's getting an error from an older browser, or could even be having multiple errors causing it to "not work". Just trying to rule out all possibilities. – Josh Ribakoff Aug 17 '15 at 21:02