嵌入作用域是否可以访问指令作用域

San*_*ram 5 angularjs transclusion angularjs-directive angularjs-ng-transclude

继续关于 Angularjs 的困惑中的讨论 嵌入并隔离范围和绑定

<controller>

  <directive>

     transcluded html

  </directive>

</controller>
Run Code Online (Sandbox Code Playgroud)

通过应用程序中的上述一般结构,链接讨论中暗示嵌入作用域基本上是从父作用域(控制器)继承的,并且嵌入作用域无法访问指令作用域。解释这一点的文章是http://angular-tips.com/blog/2014/03/transclusion-and-scopes/

我的问题是 - 嵌入范围是否可以访问指令范围?

根据上面的文章,如果我们在指令的链接函数中使用 transinclude 函数并将代码编写为:

transclude(scope, function(clone, scope) {
    element.append(clone);
  });
Run Code Online (Sandbox Code Playgroud)

这真的有效吗?我在我的应用程序上尝试了同样的操作,但它不起作用。这是我一直在使用的代码。

指令定义:

(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);
              });


            }

        };
    }

})();
Run Code Online (Sandbox Code Playgroud)

向导卡.html -

<div class="wizardContainer">
   {{bla}}
</div>
Run Code Online (Sandbox Code Playgroud)

打开 html 时,范围变量范围为空。有人能告诉我为什么会发生这种情况吗?

上述问题已解决,并更新了新问题:

现在我尝试使用多槽嵌入来执行此操作,但它不起作用。

指令定义:

(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);
                  });


            }

        };
    }

})();
Run Code Online (Sandbox Code Playgroud)

向导卡.html -

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

    <div ng-transclude="inprogress">
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)

正在使用的指令 -

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

    </completed>

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

</wizard-card>
Run Code Online (Sandbox Code Playgroud)

这也给了我一个空白,scope.$id 给了我另一个值(与指令不同)。

根据这个概念,它应该以相同的方式用于多时隙嵌入。但我不明白为什么它不能使用这段代码。

geo*_*awg 1

出于调试目的,您可以放入{{$id}}模板。$interpolation如果工作正常,这将公开范围 ID 。

您错误地使用了 double equals ==。相反,做scope.bla = "ajcnasc". JSFiddle 上的DEMO 。

是否可以将嵌入元素绑定到指令范围?

是的。

指令模板中使用的指令ng-transclude将绑定到该指令的父范围。但是,通过使用指令自身的作用域在指令的链接函数中执行嵌入,您可以将嵌入的元素绑定到指令作用域。