Computer >> Computer tutorials >  >> Programming >> Javascript

How can I invoke encodeURIComponent from AngularJS template in HTML?


To create a filter that invokes encodeURIComponent, the following is the HTML −

<div ng-app = "app">
   <a href = "/foo/{{ 'bar&baz' | encodeURIComponent }}">Demo</a>
</div>

The following is our Angular −

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