admin管理员组文章数量:1023018
Let's say I have this config:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/partials/index.html',
controller: 'defaultCtrl'
})
.when('/other', {
templateUrl: 'app/partials/other.html',
controller: 'errorCtrl'
})
.otherwise({
templateUrl: 'app/partials/404.html'
});
}
]);
I'm looking for a place to do some basic, mon maintenance code before the router calls the route. Say I'd like to clear the console log via console.clear()
every time the route is changed. How and where would be the best place in the code to do that?
Let's say I have this config:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/partials/index.html',
controller: 'defaultCtrl'
})
.when('/other', {
templateUrl: 'app/partials/other.html',
controller: 'errorCtrl'
})
.otherwise({
templateUrl: 'app/partials/404.html'
});
}
]);
I'm looking for a place to do some basic, mon maintenance code before the router calls the route. Say I'd like to clear the console log via console.clear()
every time the route is changed. How and where would be the best place in the code to do that?
1 Answer
Reset to default 8The $route
service raise events like $routeChangeStart
which you can use to perform such tasks. You can implement them using the $scope.$on method. Someting like
$scope.$on('$routeChangeStart',function(angularEvent,next,current) {
//do you work here
});
Read the $route
documentation to get idea on this and other such events.
Also $routeProvider
configuration method when
also can take a parameter resolve
which actually is used to setup dependencies before the route is resolved. This resolve
object map can also be used to achieve what you want to do.
Let's say I have this config:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/partials/index.html',
controller: 'defaultCtrl'
})
.when('/other', {
templateUrl: 'app/partials/other.html',
controller: 'errorCtrl'
})
.otherwise({
templateUrl: 'app/partials/404.html'
});
}
]);
I'm looking for a place to do some basic, mon maintenance code before the router calls the route. Say I'd like to clear the console log via console.clear()
every time the route is changed. How and where would be the best place in the code to do that?
Let's say I have this config:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/partials/index.html',
controller: 'defaultCtrl'
})
.when('/other', {
templateUrl: 'app/partials/other.html',
controller: 'errorCtrl'
})
.otherwise({
templateUrl: 'app/partials/404.html'
});
}
]);
I'm looking for a place to do some basic, mon maintenance code before the router calls the route. Say I'd like to clear the console log via console.clear()
every time the route is changed. How and where would be the best place in the code to do that?
1 Answer
Reset to default 8The $route
service raise events like $routeChangeStart
which you can use to perform such tasks. You can implement them using the $scope.$on method. Someting like
$scope.$on('$routeChangeStart',function(angularEvent,next,current) {
//do you work here
});
Read the $route
documentation to get idea on this and other such events.
Also $routeProvider
configuration method when
also can take a parameter resolve
which actually is used to setup dependencies before the route is resolved. This resolve
object map can also be used to achieve what you want to do.
本文标签:
版权声明:本文标题:javascript - How do I get the angularJS routeProvider to perform an action before the route change? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745577935a2157134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论