小编San*_*ram的帖子

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

继续关于 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 时,范围变量范围为空。有人能告诉我为什么会发生这种情况吗?

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

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

angularjs transclusion angularjs-directive angularjs-ng-transclude

5
推荐指数
1
解决办法
4041
查看次数