Encountering Error in Angularjs : [ngTransclude:orphan] Illegal use of ngTransclude directive in the template -
i got piece of code directive inside directive:
<script> app.directive('testchart', function () { return { restrict: 'e', transclude: true, controlleras: 'chartctrl', template: '<div><ng-transclude></ng-transclude></div>', controller: ['$scope', '$element', '$attrs', '$compile', function chartcontroller($scope, $element, $attrs, $compile) { $scope.chartid = $scope.$id; let content = angular.element('<div><div id="container'+ $scope.chartid + '"></div></div>'); $element.append(content); $compile($element.contents())($scope); //<---- recompilation var html = $element.html(); var hc = highcharts.chart('container' + $scope.chartid, { }); }] }; }) </script> https://www.w3schools.com/code/tryit.asp?filename=fikiwzchgppd
when adding template: <ng-transclude></ng-transclude> template error: error: [ngtransclude:orphan] illegal use of ngtransclude directive in template! no parent directive requires transclusion found.
what's cause of error? i've looked through other questions same problem unable apply solutions problem.
recompilation line in testchart directive causing problem. before execution of line angular created (& compiled) testchart directive as:
<div ng-transclude=""></div> later on appending element & compiling treats simple div element having ng-transclude directive, default showing orphan ngtransclude directive error. so, remove line directive definition, , else working fine. directive definition (same excluding recompilation line)
app.directive('testchart', function() { return { restrict: 'e', transclude: true, controlleras: 'chartctrl', template: '<div><ng-transclude></ng-transclude></div>', controller: ['$scope', '$element', '$attrs', '$compile', function chartcontroller($scope, $element, $attrs, $compile) { $scope.chartid = $scope.$id; let content = angular.element('<div><div id="container' + $scope.chartid + '"></div></div>'); $element.append(content); //$compile($element.contents())($scope); //<---- recompilation var html = $element.html(); console.log(html); var hc = highcharts.chart('container' + $scope.chartid, { }); }] }; }) in cases you'll need compulsory compilation, before appending directive template here ng-transclude used.
Comments
Post a Comment