admin管理员组文章数量:1390519
I'm trying to build a "chart widget" directive with a clone button that will clone the chart and append it right after the original.
The directive looks like this:
app.directive("chartWidget", function ($pile) {
return {
restrict: "A",
transclude: true,
templateUrl: 'chart-widget-template',
link: function ($scope, element, attrs) {
$scope.clone = function () {
var clone = $pile("chart-widget-template")($scope);
element.after(clone);
};
}
};
});
With the template as follows:
<script type="text/ng-template" id="chart-widget-template">
<section class="widget chart-widget">
<i ng-click="clone()" class="widget-clone-handle"></i>
<div class="body no-margin">
<div ng-transclude></div>
</div>
</section>
</script>
And usage as follows:
<div data-chart-widget>
<div data-some-specific-chart-directive></div>
</div>
At first I tested the cloning to work without transclusion succesfully. However, once I add the transclusion in the mix, cloning it produces this error:
Error: [ngTransclude:orphan] Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found. Element:
The angular docs for $pile state that the transcludeFn parameter to $pile is depracated (not that I know how it works anyways), so how would I acplish this?
To clarify, I want the original transclusion aka:
<div data-some-specific-chart-directive></div>
to be included in each clone.
I'm trying to build a "chart widget" directive with a clone button that will clone the chart and append it right after the original.
The directive looks like this:
app.directive("chartWidget", function ($pile) {
return {
restrict: "A",
transclude: true,
templateUrl: 'chart-widget-template',
link: function ($scope, element, attrs) {
$scope.clone = function () {
var clone = $pile("chart-widget-template")($scope);
element.after(clone);
};
}
};
});
With the template as follows:
<script type="text/ng-template" id="chart-widget-template">
<section class="widget chart-widget">
<i ng-click="clone()" class="widget-clone-handle"></i>
<div class="body no-margin">
<div ng-transclude></div>
</div>
</section>
</script>
And usage as follows:
<div data-chart-widget>
<div data-some-specific-chart-directive></div>
</div>
At first I tested the cloning to work without transclusion succesfully. However, once I add the transclusion in the mix, cloning it produces this error:
Error: [ngTransclude:orphan] Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found. Element:
The angular docs for $pile state that the transcludeFn parameter to $pile is depracated (not that I know how it works anyways), so how would I acplish this?
To clarify, I want the original transclusion aka:
<div data-some-specific-chart-directive></div>
to be included in each clone.
Share Improve this question edited Feb 28, 2014 at 0:10 Ilan Frumer 32.4k8 gold badges72 silver badges84 bronze badges asked Feb 27, 2014 at 23:34 parliamentparliament 23k39 gold badges155 silver badges247 bronze badges1 Answer
Reset to default 7Here is a demo plunker: http://plnkr.co/edit/ktEOsswQx3FyJ2pISqey?p=preview
You need to use the 5th argument of the linking function aka $transclude:
app.directive("chartWidget", function () {
return {
restrict: "A",
transclude: true,
templateUrl: 'chart-widget-template',
link: function ($scope, element, attrs, ctrl, $transclude) {
$scope.clone = function () {
$transclude(function(clone){
element.after(clone);
});
};
}
};
});
The transclude function that is passed to the pile function is deperecated
See this mit: https://github./angular/angular.js/mit/90f87072e83234ae366cfeb3c281503c31dad738
Another plunker: http://plnkr.co/edit/0MJtWpXqLzOMuuA3DX0r?p=preview
- use
$templateCache.get
to load the template - pass a
transcludeFn
as the second argument to$pile
Directive:
app.directive("chartWidget", function ($pile, $templateCache) {
return {
restrict: "A",
transclude: true,
templateUrl: 'chart-widget-template',
link: function (scope, element, attrs, ctrl, $transclude) {
var template = $templateCache.get('chart-widget-template');
scope.clone = function () {
var clone = $pile(template,$transclude)(scope);
element.after(clone);
};
}
};
});
版权声明:本文标题:javascript - Angular directive that can clone itself using $compile - Illegal use of transclusion - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744683003a2619527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论