Filter to return items.indexOf(selectedItem)

4,150 views
Skip to first unread message

Carsten Kraus

unread,
Oct 9, 2012, 2:50:55 PM10/9/12
to ang...@googlegroups.com
Hey guys,

sorry, if this an obvious one, cant seem to find an answer right now.
In an attribute of a directive I'd like to use an expression which should return the array index of a certain object.

Say, in my scope I have:
$scope.items = ['foo', 'bar', 'spam'];
$scope.selectedItem = 'spam';

Now in HTML I'd like to do sth like:
<myDir selectedIndex="items | indexOf: selectedItem"></myDir>

Does a filter like this exist already or do I write my own? Am I again doing everything totally wrong altogether? : )

Thank you!
Carsten

Pawel Kozlowski

unread,
Oct 9, 2012, 2:55:04 PM10/9/12
to ang...@googlegroups.com
Hi!

Write your own :-)

Or simply do:

<myDir selectedIndex="items.indexOf(selectedItem)"></myDir>

Personally I don't like putting too much logic in templates (can't be
tested!), so I would probably move this to a controller function.

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Carsten Kraus

unread,
Oct 10, 2012, 10:58:16 AM10/10/12
to ang...@googlegroups.com
Pawel, thanks - didnt know that the binding expressions actually get eval'ed so I can use, well, javascript : )

Personally I don't like putting too much logic in templates (can't be 
tested!)

Good point. However, my use case is even more 'un-business' than the backend example on the angular homepage, so I hope for now thats ok.
<tr ng-repeat="project in projects | filter:search | orderBy:'name'">

Thanks again & cheers, 
Carsten

Pawel Kozlowski

unread,
Oct 10, 2012, 1:36:30 PM10/10/12
to ang...@googlegroups.com
Hi!

On Wed, Oct 10, 2012 at 4:58 PM, Carsten Kraus <carste...@gmail.com> wrote:
> Pawel, thanks - didnt know that the binding expressions actually get eval'ed
> so I can use, well, javascript : )

Glad that it worked for you.

Just for the record - those expressions are not evaluated in the sense
of JS eval().
AngularJS has its own parser for expressions. Most of the time it
behaves like JS with 2 notable differences:
- you can't use conditionals (if / ternary)
- it will never fail on undefined variables

Here is more: http://docs.angularjs.org/guide/expression

Cheers,
Pawel

Carsten Kraus

unread,
Oct 16, 2012, 10:45:03 AM10/16/12
to ang...@googlegroups.com
Hi Pawel,

just saw your post(somehow notification mail didnt come through): very useful information, thanks a lot again! : )

Cheers, Carsten
Reply all
Reply to author
Forward
0 new messages