我只是在尝试使用angulat location,但不知道它在什么地方不起作用=>
app.controller("maintainenceCtrl", function ($scope, $window, $sessionStorage, ArbreeService, $location) {
angular.element(document).ready(function () {
var absUrl = $location.search();
console.log(absUrl);
});});和我的url==> http://localhost:49696/Team/Maintenance?selectionCriteria=bulkinvite
但是我不知道为什么不使用返回空object==>console的==> ....
发布于 2017-07-03 18:46:39
确保您启用了
$locationProvider.html5Mode(true); 然后尝试下面的方法。这是从query string获取URL值的100%正确方法
var search= $location.search();
if(search !=null)
{
var objectValue = search.selectionCriteria
}Update1
有没有办法不使用angular路由或ui路由,只获取参数值?
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var queryValue = getParameterByName('selectionCriteria');参考自:Getting values from query string in an url using AngularJS $location
更新2
你可以使用$routeParams
使用ngRoute从URL中获取参数。这意味着您需要将作为依赖项包含在应用程序中。更多关于如何在官方ngRoute documentation上执行此操作的信息。
这个问题的解决方案是:
// You need to add 'ngRoute' as a dependency in your app
angular.module('ngApp', ['ngRoute'])
.config(function ($routeProvider, $locationProvider) {
// configure the routing rules here
$routeProvider.when('/backend/:type/:id', {
controller: 'PagesCtrl'
});
// enable HTML5mode to disable hashbang urls
$locationProvider.html5Mode(true);
})
.controller('PagesCtrl', function ($routeParams) {
console.log($routeParams.id, $routeParams.type);
});如果您不启用$locationProvider.html5Mode(true);。Urls将使用hashbang(/#/)。
有关路由的更多信息可以在官方angular $route API documentation上找到。
旁注:这个问题回答了如何使用ng-实现这一点,但是我建议使用进行路由。它更灵活,提供更多功能,文档很棒,被认为是angular最好的路由库。
https://stackoverflow.com/questions/44883539
复制相似问题