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



在ma resource text watch Directive中,我调用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;
}

最新更新