2015-11-25 52 views
0

我試圖聲明一個指令,它顯然是正確的,但是當我在html中加載它時,什麼都不會發生。AngularJS - 這是寫一個指令的正確方法嗎?

這是代碼:

(function() { 
    'use strict'; 

    var app = angular 
     .module('App'); 

    app.directive('directiveFunction', directiveFunction); 

    function directiveFunction(){ 
     return { 
      restrict: 'EA', 
      replace: true, 
      template: '<div>Example</div>', 
      controller: directiveController, 
      controllerAs: 'example', 
      bindToController: true, 
      link: linkFunction 
     } 
    } 

    linkFunction.$inject = ['$scope', 'elem', 'attrs', 'ctrl']; 

    function linkFunction(scope, element, attrs, ctrl) {} 

    function directiveController() { 
     var example = this; 
    } 

})(); 

我稱這種現象HTML作爲<directive-function></directive-function>但不起任何作用。

+0

首先你的代碼工作,第二 - 不要在linkFunction上使用$ inject - 你不能在這裏注入。 –

+0

@PetrAveryanov它的工作原理,但我沒有在HTML中看到任何東西,也許文件位置錯誤。 – forkfork

回答

1

我創建了一個搗鼓你..你正在做的都好,我認爲你正在使用它像

https://jsbin.com/koporel/edit?html,js,output

<directiveFunction></directiveFunction> 

沒有,使用-那裏情況變化,比如

<directive-function></directive-function> 
+0

我也使用''標籤,這就是爲什麼我很困惑。 – forkfork

+0

但是它在jsbin小提琴中工作 –

+0

角度自動隱蔽駱駝的情況下'-' – user1532669

相關問題