解析一个查询字符串AngularJS不使用html5mode最好的方法 [英] Best way to parse a query string with AngularJS without using html5mode
问题描述
什么是在分析角的查询字符串的最好办法,而不使用的 html5mode ? (不使用html5mode,因为我们需要支持旧的浏览器的)
What's the best way to parse a query string in Angular without using html5mode? (Not using html5mode because we need to support older browsers)
我得到同样的结果将是不确定我是否使用哈希:
I get the same undefined results whether or not I use a hash:
http://localhost/test?param1=abc¶m2=def
http://localhost/test#/param1=abc¶m2=def
$ routeParams和$ location.search()都返回未定义的:
$routeParams and $location.search() both return undefined:
var app = angular.module('plunker', ["ngRoute"]);
app.controller('MainCtrl', ["$scope", "$routeParams", "$location",
function($scope, $routeParams, $location) {
console.log($routeParams, $routeParams.abc); //undefined, undefined
console.log($location.search(), $location.search().abc); //undefined, undefined
}]);
我可以解析window.location.search自己,但我希望有一个更好的办法做到这一点的角度。
I can parse the window.location.search myself, but I'm hoping there is a better way to do it in Angular.
Plnkr:<一href=\"http://plnkr.co/edit/alBGFAkfqncVyK7iv8Ia?p=$p$pview\">http://plnkr.co/edit/alBGFAkfqncVyK7iv8Ia?p=$p$pview
我读过<一个href=\"http://stackoverflow.com/questions/11063673/whats-the-most-concise-way-to-read-query-parameters-in-angularjs\">this发布并没有找到一个解决方案。我必须失去了一些东西。感谢您的帮助。
I've read this post and haven't found a solution. I must be missing something. Thanks for the help.
推荐答案
的查询参数应哈希去后:
the query params should go after the hash:
的http://本地主机/测试#/参数1 = ABC&放大器;参数2 = DEF
这应该允许 $ location.search()
来返回一个对象,如:
this should allow $location.search()
to return an object like:
{
param1: 'abc',
param2: 'def'
}
这篇关于解析一个查询字符串AngularJS不使用html5mode最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!