在Angularjs中只运行一次方法

时间:2014-11-04 23:16:41

标签: javascript angularjs

我有一个场景,我想在我的网站加载时只运行一次function(进行一些安全检查)。但我不知道如何用Angular方式实现这一点。

我尝试过:

.run //(not sure about this)

angular.element(document).ready

请用Angular的方式指导我。

1 个答案:

答案 0 :(得分:3)

我认为你的第一个猜测(.run)最接近。在Module Loading & Dependencies部分,它讨论了配置块和运行块:

  

模块是配置和运行块的集合,它们在引导过程中应用于应用程序。在最简单的形式中,该模块由两种块的集合组成:

     
      
  1. 配置块 - 在提供程序注册和配置阶段执行。只有提供程序和常量才能注入配置块。这是为了防止在完全配置服务之前意外实例化服务。
  2.   
  3. 运行块 - 在创建注入器后执行并用于启动应用程序。只有实例和常量才能注入运行块。这是为了防止在应用程序运行时进一步进行系统配置。
  4.   
angular.module('myModule', []).
config(function(injectables) { // provider-injector
  // This is an example of config block.
  // You can have as many of these as you want.
  // You can only inject Providers (not instances)
  // into config blocks.
}).
run(function(injectables) { // instance-injector
  // This is an example of a run block.
  // You can have as many of these as you want.
  // You can only inject instances (not Providers)
  // into run blocks
});
相关问题