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?
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).
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 .
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With