有没有办法为html节点别名范围变量?

时间:2016-11-27 13:24:58

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

我有一个指令myDirective,它有一个模板并超越了html(transclude: true) myDirective模板有一个转发器ng-repeat="item in transformSourceData",转换后的模板使用item来呈现页面,现在我的问题是如何在转换的html中使用item而不说$parent.item 我想要init-my-scope="item=$parent.item"这样的东西,这样我就不需要在我的转换html中使用$ parent.item了。

这样做的原因是我需要我的transcluded html来保留它的原始范围,因为它绑定到它的原始范围并且只使用来自父级的item,并且它还插入来自绑定模型的一些字符串评估范围为!item.property

这是结构:

myModule.directive('myDirective', function() {
var directive = {
        bindToController: true,
        controller: myDirectiveController,
        controllerAs: 'vm',
        restrict: 'E',
        transclude: true,
        scope: {
            data: '=sourceData',
        },
        template: '<div ng-repeat="item in transformSourceData"ng-transclude></div>'
    };
    return directive;
});

并在我的模板中:

<my-directive source-data="someData"> 
    <span>{{$parent.item.property}}</span>

    <!--This is what I want to use-->
    <!--<span>{{item.property}}</span>-->

    <span ng-if="{{originScope.someCondition}}>{{originScope.data}}</span>
</my-directive>
上面示例originScope.someCondition中的

将评估为!item.property,但item.property将无法使用,并且可以使用$parent.item.property检索该值 因此,当您遇到item $parent.item时,我需要以某种方式告诉我的模板,并且绑定到$parent.item.xyz的所有地方都会绑定到item.xyz

0 个答案:

没有答案
相关问题