我的问题是;当条件满足/等于 true 时,如何使用 ng-if 或其他 Angular 指令从我的 View 调用 Controller 函数?
像这样:
<div ng-if="dataHasBeenLoaded == 'true'" ng-init="configureWordCloudGraph()"></div>
这就是我想要实现的目标:
当我的数据已加载并通过 API 调用检索时,我想将 $scope
变量 ($scope.dataHasBeenLoaded = true
;) 设置为 true 。当这个 $scope
变量 === true 时,它会在我的 DOM 中进行计算,然后在我的 Controller 中调用一个函数 configureWordCloudGraph()
:
$scope.configureWordCloudGraph = function () {
if ($scope.dataHasBeenLoaded) {
var data = $scope.wordCloudData;
$scope.twitterProfileWords = WordCloud.setUpTags(data.words);
}
}
这是我的观点:
<div ng-controller="TwitterWordCloudController">
<div id="word">
<og-data-box heading="Most used words on Twitter" link="" uid="socialMentionsMeta" description="">
<div class="dataStatus" ng-show="!dataContent">{{dataStatus}}<og-loading-indicator></og-loading-indicator></div>
<div class="dataContent" ng-show="dataContent" ng-mouseover="showGraphTrainingInfo()">
<og-word-cloud words="twitterProfileWords"></og-word-cloud>
<div ng-if="dataHasBeenLoaded == 'true'" ng-init="configureWordCloudGraph()"></div>
</div>
</og-data-box>
</div>
</div>
最佳答案
简单的方法可以是观察 dataHasBeenLoaded 并在 true 时启动 configureWordCloudGraph :
var deregister = $scope.$watch('dataHasBeenLoaded', function() {
if($scope.dataHasBeenLoaded) {
configureWordCloudGraph();
deregister();
}
})
关于javascript - Angular : How can I call a controller function from an HTML view using ng-if or another Angular directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41121602/