routeparams 中的特殊字符在 angularjs 中执行控制器两次 [英] Special Characters in routeparams executing controller twice in angularjs
问题描述
如何将关键字(包含特殊字符)作为 $routeParam 传递给 AngularJS 应用程序?
How can I pass keyword (containing special characters) as a $routeParam to AngularJS App?
$routeProvider.when('/search/:keyword', {templateUrl:'someURL', controller:SearchCtrl})
这个关键字可以包含特殊字符.
This keyword can contain special characters.
因此,在重定向到此页面之前,我对关键字"做了 encodeURIComponent.
So, I did encodeURIComponent to the "keyword" before redirecting to this page.
当这个关键字"有一些特殊字符如$、@、&、逗号"等时,控制器执行两次.
When this "keyword" has some special characters such as "$, @, &, comma" etc. then the controller is executing twice.
例如:如果关键字有$"符号,控制器
EX: If the keyword has '$' symbol, the Controller
- 使用该符号的编码形式执行第一个 (#/search/%24)
- 使用实际符号 (#/search/$) 执行第二个
这不会发生在克拉('^')符号的情况下.
This does not happen in the case of carat('^') symbol.
我是否遗漏了某个地方,或者我的方法不正确.
Am I missing out somewhere or Is my approach incorrect.
推荐答案
使用变通方法处理此类场景:
Using a workaround to deal with this kind of scenario:
utils.encodeUriQuery = function (val) {
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace(/%26/gi, '&').
replace(/%3D/gi, '=').
replace(/%2B/gi, '+');
}
在我将搜索关键字传递给 URL 之前调用上述函数
Calling the above function before I pass the search keyword to the URL
$window.location.href = "/#/search/" + utils.encodeUriQuery(searchKeyword);
注意:utils 是一个工厂.
NOTE: utils is a factory.
这篇关于routeparams 中的特殊字符在 angularjs 中执行控制器两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!