AngularJS指令转换父范围

时间:2017-08-22 08:54:31

标签: angularjs angularjs-scope angularjs-ng-transclude

我正在制定一个指令,我正在使用 transclude ,因此在其中使用指令use的内部元素

让我们说这是我的指令的视图

<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不是非常易读且清晰...... 还有其他选择吗?

由于

1 个答案:

答案 0 :(得分:0)

您的指示似乎非常具体 如何将父级传递给指令呢?

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

在指令中:

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

然后在您的隔离范围声明中添加object: '<'

相关问题