Filter string with scope property as filter name

24 views
Skip to first unread message

Johnny Hauser

unread,
Jan 31, 2014, 7:42:34 PM1/31/14
to ang...@googlegroups.com
It seems like filtering within the handlebars according to a scope property value should be possible, but I can't find a way.

Here's a very simple demo of the issue: http://jsbin.com/acOdUsum/11/edit

    {{ 'make this uppercase.' | filter:foo }}

    $scope.foo = 'uppercase';

Of course, this doesn't work. It is looking for a filter named "foo". The only working solution I know of is this:

    {{ bar('make this uppercase.') }}

    $scope.bar= function(value) {
      return $filter($scope.foo)(value);
    }

Can anyone confirm that inline filtering is impossible here or otherwise propose the correct solution?

Sander Elias

unread,
Jan 31, 2014, 10:57:41 PM1/31/14
to ang...@googlegroups.com
Hi Johnny,

Thats not how filters work, you need to create a filter to be able to use it;
have a look at this: http://jsbin.com/acOdUsum/13/

Sander Elias

unread,
Jan 31, 2014, 11:03:11 PM1/31/14
to ang...@googlegroups.com
Hi Again,

After rereading your question, it sunk in! 
You want something like this: http://jsbin.com/acOdUsum/14/

Regards
Sander

Johnny Hauser

unread,
Jan 31, 2014, 11:15:46 PM1/31/14
to ang...@googlegroups.com
Right, I included that sample code just to kind of make the point. Making a filter to do the dynamic filtering seems to be the best way to go, but it just seems a bit awkward considering how generally dynamic Angular is. Anyway, I believe your suggestion confirms my suspicion that there isn't a sort of "native" way to filter inline like this. Thanks!


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/f1oLd_e3_ds/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages