簡體   English   中英

AngularJS指令包含父范圍

[英]AngularJS Directive Transclude parent scope

我正在處理一個指令,並且正在使用transclude,因此該指令使用的內部元素在其內部使用。

假設這是我指令的視圖

<div>
  <div ng-repeat="opt in options">
    <ng-transclude></ng-transclude>
  </div>
</div>

指令

app.directive("myDirective", function(){
    return {
        restrict: "E",
        transclude: true,
        templateUrl: 'my-directive.html',
        scope: {
          options: '='
        }
    };
  });

並簡單地使用它:

<my-directive options="someOptions">
     <p>{{someObject[$parent.opt]}}</p>
</my-directive>

這樣很好。 這個解決方案的我的問題是$parent.opt可讀性和清晰度不高...還有其他選擇嗎?

謝謝

您的指令似乎是一個非常具體的指令。
將父級也傳遞給指令怎么樣?

<my-directive options="someOptions" object="someObject"></my-directive>

在指令中:

<div>
  <div ng-repeat="opt in options">
    <p>{{object[opt]}}</p>
  </div>
</div>

然后在隔離的范圍聲明中添加object: '<'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM