我对AngularJS指令链接功能有疑问。它没有被调用,也没有任何错误。指令返回的模板也没有渲染:(哪里应该有问题?谢谢你的答案!
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>
答案 0 :(得分:2)
默认情况下,指令仅适用于Element和Attribute('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。