How can I change string interpolation display, the 'angular way'?

33 views
Skip to first unread message

Vaughan Hilts

unread,
Jun 15, 2014, 6:50:28 PM6/15/14
to ang...@googlegroups.com
I have an HTTP rest call that fetches a number from an endpoint. It will sometimes return '-1' to indicate that the number is 'unknown' (it's an episode count). Right now, I just use string interpolation? {{item.episodes}}?, but this displays a nasty '-1'. How can I get a clean 'Unknown', the angular way? One obvious way is at fetch time iterate the items, and assign all -1's to 'Unknown'. 

Naveen Kohli

unread,
Jun 15, 2014, 6:58:19 PM6/15/14
to ang...@googlegroups.com
You can implement a custom filter. That filter will return the value formatted the way you want.

{{item.episodes|displayEpisodeCount}}

You can see example of a custom filter from following post.


The example is at the bottom of this post.

Naveen Kohli

unread,
Jun 15, 2014, 7:00:12 PM6/15/14
to ang...@googlegroups.com
Something like in your case will be..

MyApp.filter('displayEpisodeCount', [function () {
    return function (input) { (input == -1) ? "Unknown" : input;
    };
}]);

Vaughan Hilts

unread,
Jun 15, 2014, 7:01:46 PM6/15/14
to ang...@googlegroups.com
Yup, seems very straight forward! I just couldn't get the keywords right.

Thanks Naveen 


--
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/ZwzrUqH0z1E/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/d/optout.

Reply all
Reply to author
Forward
0 new messages