如何在AngularJs的每个页面中运行一个函数

时间:2021-03-09 01:09:05

I have created a function that validates if a Cookie exists and I want to run this function in every page using angularjs. I just can't seem to make the function run. Should I put it the module on in a new controller?

我创建了一个函数来验证Cookie是否存在,并且我想在每个页面中使用angularjs运行此函数。我似乎无法使功能运行。我应该将模块放在新控制器中吗?

This is how far I reached:

这是我到达的距离:

  angular.module('myApp', ['ngCookies']).
    config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/products', {templateUrl: '/tmpl/products.html', controller: Ctrl}).
        otherwise({redirectTo: '/index'})
  }]).run( function($rootScope, $location) {

 //should I call it here?
 //validateCookie();


});

function validateCookie($scope, $cookieStore, $http){


}

4 个解决方案

#1


36  

I think there would be a couple ways of solving this. If you want to cause this validation to happen every time you change routes (which means it will run when the application first starts as well as on every page that you go to within the application), you could do something like this:

我想有几种方法可以解决这个问题。如果您希望每次更改路由时都会发生此验证(这意味着它将在应用程序首次启动时以及在您在应用程序中的每个页面上运行),您可以执行以下操作:

angular.module('myApp', ['ngCookies']).
config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/index', {templateUrl: '/tmpl/index.html', controller: IndexCtrl}).
        when('/products', {templateUrl: '/tmpl/products.html', controller: Ctrl}).
        otherwise({redirectTo: '/index'})
}])
.run(function($rootScope, validateCookie) {
    $rootScope.$on('$routeChangeSuccess', function () {
        validateCookie($rootScope);
    })
})
.factory('validateCookie', function($cookieStore, $http){
    return function(scope) {
        // Validate the cookie here...
    }
})

If you don't need to run on every route change, you could just change the "run" function:

如果您不需要在每次路由更改时运行,您只需更改“运行”功能:

.run(function($rootScope, validateCookie) {
    validateCookie($rootScope);
})

#2


1  

Perhaps you can tie into the route events (routeChangeStart) to test this before the route changes? If test fails, redirect.

也许你可以绑定到路由事件(routeChangeStart)来测试路由更改之前?如果测试失败,则重定向。

http://docs.angularjs.org/api/ng.$route

http://docs.angularjs.org/api/ng.$ro​​ute

Discussion on canceling a route change: https://groups.google.com/forum/?fromgroups=#!topic/angular/-yPBLMJQO_Q

关于取消路线变更的讨论:https://groups.google.com/forum/?fromgroups =#!topic / angular / -yPBLMJQO_Q

#3


1  

I'd create a service that you will want to inject into your controllers. The angular site has a good example of how to do this: Creating Services. Normally if you want to use some logic in many places creating a service is the way to do it.

我将创建一个您想要注入控制器的服务。角度站点有一个很好的示例,说明如何执行此操作:创建服务。通常,如果您想在许多地方使用某些逻辑,那么创建服务就是这样做的方法。

#4


1  

I would investigate combining the routeProvider resolve property and using Services.

我会研究组合routeProvider resolve属性和使用Services。

Overview video

http://www.youtube.com/watch?v=Kr1qZ8Ik9G8

http://www.youtube.com/watch?v=Kr1qZ8Ik9G8

API Details

http://docs.angularjs.org/api/ng.$routeProvider

http://docs.angularjs.org/api/ng.$ro​​uteProvider

#1


36  

I think there would be a couple ways of solving this. If you want to cause this validation to happen every time you change routes (which means it will run when the application first starts as well as on every page that you go to within the application), you could do something like this:

我想有几种方法可以解决这个问题。如果您希望每次更改路由时都会发生此验证(这意味着它将在应用程序首次启动时以及在您在应用程序中的每个页面上运行),您可以执行以下操作:

angular.module('myApp', ['ngCookies']).
config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/index', {templateUrl: '/tmpl/index.html', controller: IndexCtrl}).
        when('/products', {templateUrl: '/tmpl/products.html', controller: Ctrl}).
        otherwise({redirectTo: '/index'})
}])
.run(function($rootScope, validateCookie) {
    $rootScope.$on('$routeChangeSuccess', function () {
        validateCookie($rootScope);
    })
})
.factory('validateCookie', function($cookieStore, $http){
    return function(scope) {
        // Validate the cookie here...
    }
})

If you don't need to run on every route change, you could just change the "run" function:

如果您不需要在每次路由更改时运行,您只需更改“运行”功能:

.run(function($rootScope, validateCookie) {
    validateCookie($rootScope);
})

#2


1  

Perhaps you can tie into the route events (routeChangeStart) to test this before the route changes? If test fails, redirect.

也许你可以绑定到路由事件(routeChangeStart)来测试路由更改之前?如果测试失败,则重定向。

http://docs.angularjs.org/api/ng.$route

http://docs.angularjs.org/api/ng.$ro​​ute

Discussion on canceling a route change: https://groups.google.com/forum/?fromgroups=#!topic/angular/-yPBLMJQO_Q

关于取消路线变更的讨论:https://groups.google.com/forum/?fromgroups =#!topic / angular / -yPBLMJQO_Q

#3


1  

I'd create a service that you will want to inject into your controllers. The angular site has a good example of how to do this: Creating Services. Normally if you want to use some logic in many places creating a service is the way to do it.

我将创建一个您想要注入控制器的服务。角度站点有一个很好的示例,说明如何执行此操作:创建服务。通常,如果您想在许多地方使用某些逻辑,那么创建服务就是这样做的方法。

#4


1  

I would investigate combining the routeProvider resolve property and using Services.

我会研究组合routeProvider resolve属性和使用Services。

Overview video

http://www.youtube.com/watch?v=Kr1qZ8Ik9G8

http://www.youtube.com/watch?v=Kr1qZ8Ik9G8

API Details

http://docs.angularjs.org/api/ng.$routeProvider

http://docs.angularjs.org/api/ng.$ro​​uteProvider