Access to ng-transclude tag area in markup

I want to access the volume of the passed element VERY first time when it is inserted into a tag in a directive with an isolated area. I have access to clones of the passed item using the transclude function, but not the first time the item is inserted.

I have the following HTML

<my-directive the-name="'John Doe'"> <my-div name="myDivName"></my-div> </my-directive> 

I have two directives. I want to override the contents of my directive and be able to access a variable called "myDivName" from the scope created for the transcluded object. This variable gets its contents from the variable "theName" in the isolated area of โ€‹โ€‹the "my-directive" directive.

This is my Javascript code:

 var app = angular.module('test', []); app.directive('myDirective', function(){ return { restrict: 'E', template: '', transclude: true, scope:{ theName: '=' }, template: '<div>Hello I am My Directive and this content goes BEFORE the transclude<br><ng-transclude></ng-transclude><p>This element goes after the transclude</p></div>', link: function(scope, element, attrs, ctrl, transclude){ transclude(function (clone, transcludeScope) { transcludeScope.myDivName = scope.theName; element.append(clone);//This line shouldn't be here. I just put it to illustrate that this clone has the right value in the output. }); } } }); app.directive('myDiv', function(){ return { restrict: 'E', scope: { name: '=' }, template: '<div>{{name}}</div>' } }); 

As you can see, I use the transclude parameter of the link function in the my-directive directive to set the correct value for the myDivName variable in the transcluded scope. However, this code is only executed after Javascript has replaced the content in the tag in the "my directive" and allows me to add as human clones of the translated content the way I want (I have access to their areas so that there are no problems).

HTML output file

 <html> <head> </head> <body ng-app="test" class="ng-scope"> <my-directive the-name="'John Doe'" class="ng-isolate-scope"> <div>Hello I am My Directive and this content goes BEFORE the transclude<br> <ng-transclude> <!-- This is the very first time the transcluded element is inserted. I want access to its scope just like I have access to the clone scope in the transclude function. --> <my-div name="myDivName" class="ng-scope ng-isolate-scope"> <div class="ng-binding"> </div> </my-div> </ng-transclude> <p>This element goes after the transclude</p></div> <!-- This is a clone which scope was correctly modified to set the variable --> <my-div name="myDivName" class="ng-scope ng-isolate-scope"> <div class="ng-binding">John Doe</div></my-div> </my-directive> </body> </html> 

The problem is that the content was inserted for the first time in the tag in the "my directive". How can I access the area of โ€‹โ€‹this very first private clone?

There is an โ€œeasy wayโ€, and it should expose the variable of the isolated area of โ€‹โ€‹my directive, as this post suggests ngModel requires $ parent when inside the Transcluded html , but I do not want to interpret the parent area of โ€‹โ€‹my directive with such variables.

+5
source share
1 answer

I suggest not using ngTransclude in your template. ngTransclude is bound to a pre-isolated area (transfer area), and you are right - you do not have access to it.

Instead, use the transclude function and insert the clone yourself:

 template: '<div>Hello I am My Directive and this content goes BEFORE the transclude<br><insert-here></insert-here><p>This element goes after the transclude</p></div>', link: function(scope, element, attrs, ctrl, transclude){ transclude(function (clone, transcludeScope) { transcludeScope.myDivName = scope.theName; var e = element.find('insert-here'); e.append(clone); }); 

If you really want ngTransclude

Or, if you really want ngTransclude in your template, then the following should work:

 template: '<div>Hello I am My Directive and this content goes BEFORE the transclude<br><ng-transclude></ng-transclude><p>This element goes after the transclude</p></div>', link: function(scope, element, attrs, ctrl, transclude){ var e = element.find('ng-transclude'); transcludeScope = e.scope(); transcludeScope.myDivName = scope.theName; }); 

This solution uses jqLite to search for ngTransclude, and then calls the scope () method to get the scope of the transclusions.

0
source

Source: https://habr.com/ru/post/1234264/


All Articles