2015-11-25 53 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

相关问题