【发布时间】:2016-02-10 16:31:27
【问题描述】:
这就是我的应用的结构。
<controller>
<directive>
transcluded html
</directive>
</controller>
我在控制器中启动了一个名为“defaultShipTo”的范围变量。我正在尝试在此控制器内部的指令的嵌入 html 中访问此范围变量。根据嵌入范围的规则,嵌入范围基本上是指令的父级(即本例中的控制器)的副本。
在绑定和嵌入发生在指令内之后,我试图操纵被嵌入的 html。为此,我在指令中使用 post-link 函数。
指令代码 -
(function(){
'use strict';
angular.module('checkoutApp')
.directive('wizardCard', [wizardCardDirective]);
function wizardCardDirective(){
return {
restrict : 'E',
replace : false,
scope : {
current : '@',
checkoutStates: '='
},
transclude: true,
templateUrl: 'wizard-card.html',
compile: function(element, attributes){
console.log("compile");
return {
pre: function(scope, element, attributes, controller, transcludeFn){
console.log("pre");
},
post: function(scope, element, attributes, controller, transcludeFn){
console.log("post");
console.log(element.html())
}
}
},
};
}
})();
模板代码 - wizarrd-card.html -
<ng-transclude></<ng-transclude>
在 html 中使用的指令 -
<wizard-card current="shipping" checkout-states="checkoutStates">
{{defaultShipTo}}
</wizard-card>
当我在发布链接期间在控制台上打印出 element.html() 时,我得到以下信息:
<ng-transclude><span class="ng-binding ng-scope">
{{defaultShipTo}}
</span></ng-transclude>
我是否应该在链接后获得 defaultShipTo 的“后绑定”值?
注意:绑定最终会发生并填充值,但我不确定为什么它在链接后尚未发生。
【问题讨论】:
-
你应该在 postLink 函数中使用
console.log(transcludeFn();,它会给你嵌入的 html。
标签: angularjs angularjs-directive transclusion