zachsnow / ng-multi-transclude

ng-multi-transclude
MIT License
79 stars 19 forks source link

Loosing Scope #16

Closed littlemtravis closed 9 years ago

littlemtravis commented 9 years ago

I have setup my template, nav-template, like below.

(function () {
    'use strict';

    var sample = angular.module('sample', []).directive('navTemplate', navTemplate);

    function navTemplate() {
        var directive = {
            restrict: 'E',
            templateUrl: 'directives/nav/nav.html',
            scope: {
                versionHref: '@'
            },
            transclude: true
        };

        return directive;
    }
})();
nav/nav.html
<div ng-multi-transclude-controller>

        <div ng-multi-transclude="nav-header"></div>

        <div ng-multi-transclude="nav-menu"></div>

        <div ng-multi-transclude="nav-footer">
            <div>{{versionHref}}</div>
        </div>

</div>

Works: the nav-version directive finds the hrefPath on the scope

<body ng-init="{hrefPath='package.json'}">
        <nav-template version-href="{{hrefPath}}"></nav-template>
</body>

Doesn't Work: the nav-version directive doesn't find the hrefPath on the scope. The scope gets moved into the $$childHead.

<body ng-init="{hrefPath='package.json'}">
        <nav-template version-href="{{hrefPath}}">
            <div name="nav-footer">
                <div>This overriding template.</div>
                <div>{{versionHref}}</div>
            </div>
        </nav-template>
</body>
zachsnow commented 9 years ago

Your multi-transcluded blocks are linked in the context in which they are defined, not in context into which they are "injected". So you should use hrefPath because that's in scope. This is similar to #8 but I think it's quite reasonable that this might be expected to work, since you can see version-href right there. I'll have a look, thanks!

zachsnow commented 9 years ago

Let me know if this works for you!

littlemtravis commented 9 years ago

Take a look at this http://plnkr.co/edit/50JMsM?p=preview, don't know why I didn't write a plunk to begin with.

zachsnow commented 9 years ago

I've updated it to reflect the change I suggested above:

http://plnkr.co/edit/lv7AHh?p=preview

Hope that helps.

littlemtravis commented 9 years ago

Got it, makes sense. Thanks for the help.