Angular 指令的链接函数未被调用 [英] Angular directive's link function not being called
问题描述
我遇到了 AngularJS 指令链接函数的问题.它没有被调用,也不会抛出任何错误.指令返回中的模板也没有呈现:(应该是哪里出了问题?谢谢你的回答!
angular.module('sampleApp.game').directive('gameCanvas', function($injector) {console.log('指令正在运行');//这有效,功能链接Fn(范围,电子,属性){console.log('Link 函数不起作用 :(');//但这不是 :(};返回 {范围: {},模板:'<div class="blabla"></div>',链接:linkFn}});
我的 html 模板文件
<h1>玩游戏!</h1><p>{{ 标语 }}</p><div class="game-canvas"></div>
默认情况下,指令仅用于元素和属性 ('EA').将限制属性定义为C".最佳做法是始终明确定义它.
angular.module('sampleApp.game').directive('gameCanvas', function($injector) {console.log('指令正在运行');//这有效,功能链接Fn(范围,电子,属性){console.log('Link 函数不起作用 :(');//但这不是 :(};返回 {范围: {},限制: 'C',//'EA' 默认模板:'<div class="blabla"></div>',链接:linkFn}
});
Angular 在此处记录 - https://docs.angularjs.org/api/ng/服务/$compile#directive-definition-object.
I've got a problem with AngularJS directive link function. It's not beeing called and it doesn't throw any error. Also the template in directive's return is not rendering :( Where should be problem? Thank you for answers!
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
}
});
My html template file
<div class="jumbotron text-center">
<h1>Play a game!</h1>
<p>{{ tagline }}</p>
<div class="game-canvas"></div>
</div>
By default, directives are for Element and Attribute ('EA') only. Define the restrict attribute as 'C'. Best practice is to always define it explicitly.
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
}
});
Documented by Angular here - https://docs.angularjs.org/api/ng/service/$compile#directive-definition-object.
这篇关于Angular 指令的链接函数未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!