如何从 angularJS 模板调用encodeURIComponent?

2024-05-08

我的 Angular JS 模板中有一个块

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

但是,foo.id 属性有时可能包含时髦字符(“/”)。我想做这样的事情:

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

但它不起作用?我怎样才能解决这个问题?


您可以创建一个过滤器来调用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>

运行示例:http://jsfiddle.net/YApdK/ http://jsfiddle.net/YApdK/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 angularJS 模板调用encodeURIComponent? 的相关文章

随机推荐