如何使用 AngularJS 生成 url 编码的锚链接? [英] How to generate url encoded anchor links with AngularJS?

查看:44
本文介绍了如何使用 AngularJS 生成 url 编码的锚链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a href="#/search?query={{address}}" ng-repeat="address in addresses">
  {{address}}
</a>

如果我理解正确,生成的链接不是 url 编码的.编码 #/search?query={{address}} 的正确方法是什么?

generates links that are not url encoded if I understand correctly. What is the proper way to encode #/search?query={{address}}?

示例地址为 1260 6th Ave, New York, NY.

推荐答案

您可以在 javascript 中使用原生的 encodeURIComponent.另外,你可以把它做成一个字符串过滤器来使用它.

You can use the native encodeURIComponent in javascript. Also, you can make it into a string filter to utilize it.

这里是制作escape过滤器的例子.

Here is the example of making escape filter.

js:

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

html:

<a ng-href="#/search?query={{address | escape}}">

(更新:适应 Karlies 的答案,它使用 ng-href 而不是普通的 href)

(updated: adapting to Karlies' answer which uses ng-href instead of plain href)

这篇关于如何使用 AngularJS 生成 url 编码的锚链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆