2014-01-17 39 views
0

我想定製textAngular指令選項。

API文檔說我應該設置$ rootScope.textAngularTools。{customButton}來構建一個函數。

但是,如果我在控制器上設置,指令會告訴我屬性是未定義的。

如果我在module.run函數中設置,$ rootScope.textAngularTools是未定義的。

如何在指令初始化之前設置選項?

<text-angular to-toolbar="[['customButton']]"> 

讀取源我建議設置像這樣(的CoffeeScript)

$rootScope.textAngularTools.colourRed = 
    display: "<button ng-click='action()' ng-class='displayActiveToolClass(active)'><i class='fa fa-square' style='color: red;'></i></button>", 
    action: -> 
    console.log 'action' 
    activeState: -> 
    false 

回答

2

你做你的配置在模塊運行功能:

angular.modul('myApp', ['textAngular']) 

.run(function($rootScope){ 
    $rootScope.textAngularTools = { 
     colourRed: { 
       display: "<button ng-click='action()' ng-class='displayActiveToolClass(active)'><i class='fa fa-square' style='color: red;'></i></button>", 
       action: function(){ 
       console.log('action); 
       } 
     } 
    }; 
}) 

.controller('yourController')... 

爲什麼要把這項工作?他們擴展了他們的指令中現有的textAngularTools對象:

$rootScope.textAngularTools != null)? $rootScope.textAngularTools : {} 
$rootScope.textAngularTools != null)? $rootScope.textAngularTools : {} 
+0

感謝您的建議,所以$ rootScope是所有控制器$ scope的角度原型?再次感謝你:) –

+1

@柴仔育是的。 – michael