2015-04-02 63 views
1

我遇到了AngularJS指令鏈接函數的問題。這不是被稱爲的,它不會拋出任何錯誤。此外,在指令的回報模板不會渲染:(應該在哪裏是什麼問題?謝謝您的回答!Angular指令的鏈接函數未被調用

angular.module('sampleApp.game').directive('gameCanvas', function($injector) {  
    console.log('Directive is working'); // this works, 

    function linkFn(scope, ele, attrs) { 
     console.log('Link function doesnt working :('); // but this not :(
    }; 

    return { 
     scope: {}, 
     template: '<div class="blabla"></div>', 
     link: linkFn 
    } 
}); 

我的HTML模板文件

<div class="jumbotron text-center"> 
    <h1>Play a game!</h1> 
    <p>{{ tagline }}</p> 
    <div class="game-canvas"></div> 
</div> 
+1

嘗試添加'restrict:「C」'或其他任何您想要的限制。 – floribon 2015-04-02 23:43:14

+0

不錯!有用!但爲什麼? :D爲什麼它沒有工作過? – user3216673 2015-04-02 23:48:15

回答

2

默認情況下,指令是對元素和屬性(」 EA'),將restrict屬性定義爲'C',最好的做法是明確地定義它

angular.module('sampleApp.game').directive('gameCanvas', function($injector) {  
console.log('Directive is working'); // this works, 

function linkFn(scope, ele, attrs) { 
    console.log('Link function doesnt working :('); // but this not :(
}; 

return { 
    scope: {}, 
    restrict: 'C', //'EA' by default 
    template: '<div class="blabla"></div>', 
    link: linkFn 
} 

});

由Angular記錄 - https://docs.angularjs.org/api/ng/service/ $ compile#directive-definition-object。

相關問題