2014-11-04 59 views
3

我有一個場景,我只想在我的網站上運行一次function(以進行一些安全檢查)。但我不知道如何以角度來實現這一點。在Angularjs中只運行一次方法

我曾嘗試:

.run //(not sure about this) 

angular.element(document).ready 

請指導我這個角度與方式。

回答

3

我認爲你的第一個猜測(.run)最接近。在上Module Loading & Dependencies的部分,它談論的配置塊和運行塊:

模塊是配置的集合,並運行其中獲得在引導過程中應用到的應用程序塊。在其最簡單的形式中,模塊包含兩種塊的集合:

  1. 配置塊 - 在提供程序註冊和配置階段執行。只有提供者和常量可以注入到配置塊中。這是爲了防止服務在完全配置之前發生意外實例化。
  2. 運行塊 - 在創建注入器後執行並用於啓動應用程序。只有實例和常量可以注入運行塊。這是爲了防止在應用程序運行時進一步進行系統配置。
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 
});