admin管理员组文章数量:1392105
In my application I'm using some query string parameters to resolve links for preselecting some data. But I would like to hide these parameters for users. For example, where I'm using a route like localhost/myapp#/settings?key=dataGrid&value=10
, I want to clear these parameters and show user a route like localhost/myapp#/settings
.
I tried something like this:
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('');
}
});
}]);
But it does not do anything. On StackOverflow I found something about using $rootScope.$Apply
, but if it try it, I get this error: [$rootScope:inprog] $digest already in progress
.
In my application I'm using some query string parameters to resolve links for preselecting some data. But I would like to hide these parameters for users. For example, where I'm using a route like localhost/myapp#/settings?key=dataGrid&value=10
, I want to clear these parameters and show user a route like localhost/myapp#/settings
.
I tried something like this:
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('');
}
});
}]);
But it does not do anything. On StackOverflow I found something about using $rootScope.$Apply
, but if it try it, I get this error: [$rootScope:inprog] $digest already in progress
.
- why passing parameter through URL ? it's not really a good idea, create a service instead. Betting 2$ that you haev a PHP background ;) – Jscti Commented Apr 3, 2014 at 15:25
- 4 @Bixi - maybe it'd be helpful to provide an example of what you mean of using a service instead. And explain why you feel it's not a good idea. – EnigmaRM Commented Apr 3, 2014 at 16:16
1 Answer
Reset to default 4Try: $locationChangeStart to handle page changes. Your code would be as follows:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
$rootScope.target = $location.search()['key']; // (equivalent) key = GET[key]
// $rootScope.target = $location.search().key; // Other solution
});
To set a parameter to the url from a controller (without use of a link), the code is as follows:
myApp.controller('MyCtrl',function($scope, $location) {
// setParam('key', 'dataGrid')
$scope.setParam = function(param, value) {
$location.search(param, value); // domain./#/page?key=dataGrid
};
});
Live example: http://jsfiddle/Chofoteddy/3wFeR/
That said, your code would be as follows:
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
var param = $location.search()['key'];
$location.path(param).search(''); // Change url and clean params
});
More info: $rootScope.$on("$locationChangeStart")
本文标签: javascriptAngularJS how to change location path in routeChangeStart eventStack Overflow
版权声明:本文标题:javascript - AngularJS: how to change location path in routeChangeStart event? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744775762a2624614.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论