Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I invoke encodeURIComponent from angularJS template?

I have a block in my angular JS template a la

<a href="#/foos/{{foo.id}}">{{foo.name}}</a> 

However, the foo.id property can sometimes contain funky characters ('/'). I want to do something like this:

<a href="#/foos/{{encodeURIComponent(foo.id)}}">{{foo.name}}</a> 

but it doens't work? How can I fix this?

like image 528
ConfusedNoob Avatar asked May 19 '13 00:05

ConfusedNoob


People also ask

What is encodeURIComponent in Angularjs?

The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

Which HTML tag is used to define the templates in Angularjs?

A static template is defined by using script tag. It must has an id attribute with a unique value and a type attribute with value text/ng-template .


1 Answers

You could create a filter that calls encodeURIComponent

E.g.

var app = angular.module('app', []); app.filter('encodeURIComponent', function() {     return window.encodeURIComponent; }); 

Then do

<a href="#/foos/{{foo.id | encodeURIComponent}}">{{foo.name}}</a> 

Running example: http://jsfiddle.net/YApdK/

like image 74
jimr Avatar answered Oct 09 '22 04:10

jimr