5

Continuing the discussion in Confused about Angularjs transcluded and isolate scopes & bindings

<controller>

  <directive>

     transcluded html

  </directive>

</controller>

With the above general structure in the app, it is implied in the linked discussion that transclusion scope is basically inherited from parent scope(controller) and that transclusion scope cannot access the directive scope. The article explaining this is http://angular-tips.com/blog/2014/03/transclusion-and-scopes/

My question is - Is it possible for the transclusion scope to access the directive scope?

according to the above article, this should be possible if we use transclude function within the link function of the directive and write code as:

transclude(scope, function(clone, scope) {
    element.append(clone);
  });

Does this actually work? I tried the same on my app, but it is not working. Here is the code that I have been using.

Directive definition:

(function(){
    'use strict';

    angular.module('checkoutApp')
        .directive('wizardCard', ['shipToService','deliveryService','billingService', wizardCardDirective]);


    function wizardCardDirective(shipToService,deliveryService,billingService){
        return {
            restrict : 'E',
            scope : {
               current : '@',
               checkoutStates: '='
            },
            transclude:true,
            templateUrl: '/UsabilitySites/Cart/Checkout/app/components/shared/wizard-card.html',
            link: function(scope, element, attrs,ctrl,transclude){


                scope.bla == "ajcnasc";

                transclude(scope, function(clone, scope) {
                    element.append(clone);
              });


            }

        };
    }

})();

wizard-card.html -

<div class="wizardContainer">
   {{bla}}
</div>

scope variable scope is coming as just empty when opening the html. Can someone tell me why this is happening?

THE ABOVE QUESTION WAS SOLVED UPDATING WITH NEW QUESTION :

Now i tried doing this with multi-slot transclusion, it is not working.

Directive definition:

(function(){
    'use strict';

    angular.module('checkoutApp')
        .directive('wizardCard', [wizardCardDirective]);


    function wizardCardDirective(){
        return {
            restrict : 'E',
            scope : {
               current : '@',
               checkoutStates: '='
            },
            transclude: {
                'completed': 'completed',
                'inprogress': 'inprogress'
              },
            templateUrl: 'wizard-card.html',
            link: function(scope, element, attrs,ctrl,transclude){

                scope.bla = "ajcnasc";

                transclude(scope, function(clone, scope) {
                        element.append(clone);
                  });


            }

        };
    }

})();

wizard-card.html -

 <div class="wizardContainer">
    <div ng-transclude="completed">
    </div>

    <div ng-transclude="inprogress">
   </div>
</div>

directive being used -

<wizard-card current="shipping" checkout-states="checkoutStates">
    <completed>
        bla {{bla}}

    </completed>

    <inprogress>
        {{bla}}
    </inprogress>

</wizard-card>

This is giving me an blank as well and the scope.$id is giving me another value (different from the directive).

According to the concept, it should work the same way for multi-slot transclusion. But i cant figure out why its not working with this code.

Arashsoft
  • 2,749
  • 5
  • 35
  • 58
Sankar Vikram
  • 81
  • 2
  • 7
  • For debugging purposes, you can put `{{$id}}` in your template. That will expose the scope ID if `$interpolation` is working properly. – georgeawg Feb 09 '16 at 18:33
  • You erroneusly used double equals `==`. Instead do `scope.bla = "ajcnasc"`. The [DEMO on JSFiddle](https://jsfiddle.net/3L21jw4r/). – georgeawg Feb 09 '16 at 18:49

1 Answers1

1

For debugging purposes, you can put {{$id}} in your template. That will expose the scope ID if $interpolation is working properly.

You erroneusly used double equals ==. Instead do scope.bla = "ajcnasc". The DEMO on JSFiddle.

Is it possible to bind transcluded elements to the directive scope?

Yes.

The ng-transclude directive used in a directive template will bind to the parent scope of the directive. But by doing the transclusion in the directive's linking function using the directive's own scope, you can bind the transcluded elements to the directive scope.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Thank you. Can you take a look at the new scenario above and see what might be the issue there. – Sankar Vikram Feb 09 '16 at 20:12
  • This worked for me except that the function I was trying to call was removed from the scope for some reason. I can put an integer in there and it stays but the function is removed :( – mjaggard Oct 20 '21 at 10:03