Javascript AngularJS:如何在routeChangeStart事件中更改位置路径?
在我的应用程序中,我使用一些查询字符串参数来解析用于预选某些数据的链接。但我想为用户隐藏这些参数。例如,当我使用像Javascript AngularJS:如何在routeChangeStart事件中更改位置路径?,javascript,angularjs,angularjs-routing,Javascript,Angularjs,Angularjs Routing,在我的应用程序中,我使用一些查询字符串参数来解析用于预选某些数据的链接。但我想为用户隐藏这些参数。例如,当我使用像localhost/myapp#/settings?key=dataGrid&value=10这样的路由时,我希望清除这些参数并向用户显示像localhost/myapp#/settings这样的路由 我试过这样的方法: angular.module('myApp') .run(['$rootScope', '$sce', '$location', '$route', fun
localhost/myapp#/settings?key=dataGrid&value=10
这样的路由时,我希望清除这些参数并向用户显示像localhost/myapp#/settings
这样的路由
我试过这样的方法:
angular.module('myApp')
.run(['$rootScope', '$sce', '$location', '$route',
function($rootScope, $sce, $location, $route) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if ($location.url().indexOf('?key=')) {
var newLocationPath = $location.url().substring(
0, $location.url().indexOf('?key='));
$location.path(newLocationPath).search('');
}
});
}]);
但它没有任何作用。在StackOverflow上,我发现了一些关于使用
$rootScope.$Apply
,但是如果它尝试使用它,我会得到以下错误:[$rootScope:inprog]$digest已经在进行中
尝试:$locationChangeStart来处理页面更改。您的代码如下:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
$rootScope.target = $location.search()['key']; // (equivalent) key = GET[key]
// $rootScope.target = $location.search().key; // Other solution
});
myApp.controller('MyCtrl',function($scope, $location) {
// setParam('key', 'dataGrid')
$scope.setParam = function(param, value) {
$location.search(param, value); // domain.com/#/page?key=dataGrid
};
});
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
var param = $location.search()['key'];
$location.path(param).search(''); // Change url and clean params
});
要从控制器(不使用链接)将参数设置为url,代码如下:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
$rootScope.target = $location.search()['key']; // (equivalent) key = GET[key]
// $rootScope.target = $location.search().key; // Other solution
});
myApp.controller('MyCtrl',function($scope, $location) {
// setParam('key', 'dataGrid')
$scope.setParam = function(param, value) {
$location.search(param, value); // domain.com/#/page?key=dataGrid
};
});
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
var param = $location.search()['key'];
$location.path(param).search(''); // Change url and clean params
});
现场示例:
也就是说,您的代码如下所示:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
$rootScope.target = $location.search()['key']; // (equivalent) key = GET[key]
// $rootScope.target = $location.search().key; // Other solution
});
myApp.controller('MyCtrl',function($scope, $location) {
// setParam('key', 'dataGrid')
$scope.setParam = function(param, value) {
$location.search(param, value); // domain.com/#/page?key=dataGrid
};
});
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
var param = $location.search()['key'];
$location.path(param).search(''); // Change url and clean params
});
更多信息:尝试:$locationChangeStart处理页面更改。您的代码如下:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
$rootScope.target = $location.search()['key']; // (equivalent) key = GET[key]
// $rootScope.target = $location.search().key; // Other solution
});
myApp.controller('MyCtrl',function($scope, $location) {
// setParam('key', 'dataGrid')
$scope.setParam = function(param, value) {
$location.search(param, value); // domain.com/#/page?key=dataGrid
};
});
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
var param = $location.search()['key'];
$location.path(param).search(''); // Change url and clean params
});
要从控制器(不使用链接)将参数设置为url,代码如下:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
$rootScope.target = $location.search()['key']; // (equivalent) key = GET[key]
// $rootScope.target = $location.search().key; // Other solution
});
myApp.controller('MyCtrl',function($scope, $location) {
// setParam('key', 'dataGrid')
$scope.setParam = function(param, value) {
$location.search(param, value); // domain.com/#/page?key=dataGrid
};
});
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
var param = $location.search()['key'];
$location.path(param).search(''); // Change url and clean params
});
现场示例:
也就是说,您的代码如下所示:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
$rootScope.target = $location.search()['key']; // (equivalent) key = GET[key]
// $rootScope.target = $location.search().key; // Other solution
});
myApp.controller('MyCtrl',function($scope, $location) {
// setParam('key', 'dataGrid')
$scope.setParam = function(param, value) {
$location.search(param, value); // domain.com/#/page?key=dataGrid
};
});
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
var param = $location.search()['key'];
$location.path(param).search(''); // Change url and clean params
});
更多信息:为什么要通过URL传递参数?这不是一个好主意,而是创建一个服务。打赌2美元你有PHP背景;)@Bixi-也许提供一个例子来说明使用服务的意义会有所帮助。并解释为什么您觉得这不是一个好主意。为什么要通过URL传递参数?这不是一个好主意,而是创建一个服务。打赌2美元你有PHP背景;)@Bixi-也许提供一个例子来说明使用服务的意义会有所帮助。并解释为什么你觉得这不是个好主意。你好,非常感谢!这里:我添加了两个函数。这很有趣,但我的第一个函数“clearUrlKeysBad”在我的项目中不起作用。也许是因为AngularJS的不同版本。。?但我的第二个函数“clearUrlKeysRigt”对我有用。设置$location.path和$location.url之间有什么不同?没错,事实上我找到了一个更好的解决方案,你在前面的评论中发表过。“$location.url”作用于浏览器中的完整地址,而“$location.path”作用于斜杠(/)。您好,非常感谢!这里:我添加了两个函数。这很有趣,但我的第一个函数“clearUrlKeysBad”在我的项目中不起作用。也许是因为AngularJS的不同版本。。?但我的第二个函数“clearUrlKeysRigt”对我有用。设置$location.path和$location.url之间有什么不同?没错,事实上我找到了一个更好的解决方案,你在前面的评论中发表过。“$location.url”作用于浏览器中的完整地址,而“$location.path”作用于斜杠(/)。