【问题标题】:Is it possible for the transclusion scope to access the directive scope嵌入范围是否可以访问指令范围
【发布时间】:2016-05-19 20:52:49
【问题描述】:

继续讨论 Confused about Angularjs transcluded and isolate scopes & bindings

<controller>

  <directive>

     transcluded html

  </directive>

</controller>

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

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

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

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

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

指令定义:

(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>

范围变量范围在打开 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);
                  });


            }

        };
    }

})();

wizard-card.html -

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

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

正在使用的指令 -

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

    </completed>

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

</wizard-card>

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

根据概念,它应该以与多槽嵌入相同的方式工作。但我不知道为什么它不能使用这段代码。

【问题讨论】:

  • 出于调试目的,您可以将{{$id}} 放入您的模板中。如果$interpolation 工作正常,这将暴露范围 ID。
  • 您错误地使用了 double 等于 ==。而是做scope.bla = "ajcnasc"DEMO on JSFiddle.

标签: angularjs angularjs-directive angularjs-ng-transclude transclusion


【解决方案1】:

出于调试目的,您可以将{{$id}} 放入您的模板中。如果$interpolation 工作正常,这将暴露作用域ID。

您错误地使用了 double 等于 ==。而是做scope.bla = "ajcnasc"DEMO on JSFiddle

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

是的。

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

【讨论】:

  • 谢谢。你能看看上面的新场景,看看那里可能有什么问题吗?
  • 这对我有用,只是我试图调用的函数由于某种原因从范围中删除了。我可以在里面放一个整数,它会保留,但函数被删除:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 2014-04-16
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
相关资源
最近更新 更多