【发布时间】:2014-07-06 08:14:40
【问题描述】:
我有这个指令:
hpDsat.directive('ngElementReady', [function() {
return {
restrict: "A",
link: function($scope, $element, $attributes) {
// put watches here.
console.log(" WHAT THE @#%*%$??? ");
$scope.$eval($attributes.ngElementReady);
}
};
}]);
我从来没有看到console.log 的输出。我正在声明这样的元素:
<div data-ng-element-ready="console.log(' ------------------------------- COMPILED! ')" data-ng-if="analysis.type" data-ng-show="showBasicHtml" data-ng-include="analysis.type+'Content.html'"></div>
是的,我在声明div 元素所在的控制器之前声明了指令。元素出现,ngShow 和 ngInclude 正常工作,加载的模板中的任何内容也正常工作(更多指令、控制器、{{expressions}} 等)。
如果我用编译函数执行它,编译函数确实工作,但仍然不是链接函数:
hpDsat.directive('ngElementReady', [function() {
return {
restrict: "A",
compile: function($element, $attributes) {
console.log("This I do see."); // THIS WORKS!!
return function($scope) {
// put watches here.
console.log("But not this. Why???"); // DOESN'T WORK!!
$scope.$eval($attributes.ngElementReady);
};
}
};
}]);
编译函数的console.log运行正常,但是返回的链接函数仍然没有被执行。
知道为什么链接函数可能不会被触发吗?
【问题讨论】:
-
发现问题。我在 Eduard Gamonal 的回答中对此进行了评论。
标签: javascript html angularjs angularjs-directive angularjs-scope