繁体   English   中英

将Angular Directive属性绑定到父控制器

[英]Binding an Angular Directive property to a parent Controller

在ma-resource-text-watch指令中,我进行了api调用以获取资源文本列表。 如果api不返回任何资源文本,我希望能够隐藏警报组件。 有谁知道我怎么能做到这一点?

<div ng-controller="IntroductionCntrl" class="hidden-print">
    <div class="container-fluid" ng-if="introductionResourceKey">
        <alert-component type="guidance">
            <span ma-resource-text-watch="{{introductionResourceKey}}"></span>
        </alert-component>
    </div>
</div>

您可以让指令接受一个回调(或表达式),该回调将在加载数据时触发。 例如,在指令定义中, scope属性可以具有:

scope: {
  onTextsLoaded: '&'
}

该指令然后可以调用:

scope.onTextsLoaded({ texts: yourTexts })

父控制器可以将表达式作为回调传递,并使用ng-show隐藏alert-component

<alert-component ng-show="dataIsLoaded && texts.length">
    <span ma-resource-text-watch="{{introductionResourceKey}}" on-texts-loaded="onTextsLoaded(texts)"></span>
</alert-component>

函数定义如下:

$scope.onTextsLoaded = function(texts) {
    $scope.dataIsLoaded = true;
    $scope.texts = texts;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM