[英]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.