Angularjs 角度:链接转置的模板
我有一个指令,该指令部分由来自父作用域的ng repeat填充,然后监听器被附加到Angularjs 角度:链接转置的模板,angularjs,angularjs-directive,transclusion,Angularjs,Angularjs Directive,Transclusion,我有一个指令,该指令部分由来自父作用域的ng repeat填充,然后监听器被附加到postLink中。但是,由于内容是转置的,因此不可用/按链接时间内插 我已经准备了一个演示: 模板: <script type="text/ng-template" id="directive.html"> <div>list item count: {{ count }} (should be {{2 + items.length }})</div>
postLink
中。但是,由于内容是转置的,因此不可用/按链接时间内插
我已经准备了一个演示:
模板:
<script type="text/ng-template" id="directive.html">
<div>list item count: {{ count }} (should be {{2 + items.length }})</div>
<div>Transcluded content: <span ng-transclude></span></div>
</script>
<div ng-controller="Ctrl">
<ul frag>
<li ng-repeat="item in items">{{ item }}</li>
<li>4</li>
<li>5</li>
</ul>
</div>
app.directive("frag", function ($http) {
return {
restrict: 'A',
transclude: true,
templateUrl: 'directive.html',
link: function (scope, element, attrs) {
scope.count = element.find("li").length;
console.log(element);
},
controller: function ($scope) {
$scope.foundB = false;
}
};
});
在这种情况下,列表项计数最终为2,而不是预期的5,即转换后的5
有人知道我该怎么做吗?我希望找到某种类型的转包后事件,我可以观察到,然后只进行链接,但找不到。您可以使用
$timeout
在摘要周期后将代码移动到执行队列的末尾:
app.directive("frag", function ($http, $timeout) {
return {
restrict: 'A',
transclude: true,
templateUrl: 'directive.html',
link: function (scope, element, attrs) {
$timeout(function() {
scope.count = element.find("li").length;
});
},
controller: function ($scope, $element) {
$scope.foundB = false;
}
};
});
Ta-我一直在使用
$timeout
作为最后手段来保存!希望(a)我错过了一些明显的东西,或者(b)有一些更整洁的东西。