angularjs是否为$routeProvider提供了一个init接口,以便我可以为每个路由执行相同的操作
现在我启动一个angularjs(版本:1.0.8)应用程序,如下所示:angularjs是否为$routeProvider提供了一个init接口,以便我可以为每个路由执行相同的操作,angularjs,Angularjs,现在我启动一个angularjs(版本:1.0.8)应用程序,如下所示: angular.module('cms', ['angularFileUpload','ui.bootstrap']). config(['$routeProvider','$httpProvider', function($routeProvider,$httpProvider) { //$httpProvider.defaults.withCredentials = true; $routePro
angular.module('cms', ['angularFileUpload','ui.bootstrap']).
config(['$routeProvider','$httpProvider', function($routeProvider,$httpProvider) {
//$httpProvider.defaults.withCredentials = true;
$routeProvider.
when('/foo',{templateUrl: 'partials/foo/index.html',resolve:{nav:function(){
renderNav();
}}}).
.when('/bar',{templateUrl: 'partials/bar/index.html',resolve:{nav:function(){
renderNav();
}}})
}]);
每次更改路由时,我都想调用函数renderNav(),那么如何使用$routeProvider执行此操作?在应用程序运行过程中,您可以监听成功的路由更改:
angular.module('cms').run(['$rootScope', function($rootScope) {
$rootScope.$on('$routeChangeSuccess', function() {
// call renderNav() which could be in a factory that you inject
});
}]);
您还可以将相同的代码放入指令中,该指令将放入视图的html中,您将在其中显示导航。在应用程序运行过程中,您可以侦听成功的路由更改:
angular.module('cms').run(['$rootScope', function($rootScope) {
$rootScope.$on('$routeChangeSuccess', function() {
// call renderNav() which could be in a factory that you inject
});
}]);
您还可以将相同的代码放入指令中,该指令将放入视图的html中,在其中显示导航。您在“解析”中使用它的方式不起作用吗?您在“解析”中使用它的方式不起作用吗?