javascript - Angular : How can I call a controller function from an HTML view using ng-if or another Angular directive

标签 javascript angularjs angularjs-controller

我的问题是;当条件满足/等于 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/

相关文章:

javascript - 如何在 AngularJS 中获取文件内容和其他细节

angularjs - ng-repeat 中的 Angular UI 工具提示未关闭

javascript - ng-repeat 不适用于 onsen ui

javascript - jQuery 移动 : Popup and vclick event

javascript - 如何在 GitHub Pages 上托管?

javascript - 事件时更改菜单颜色项

javascript - 如何触发键盘事件在textarea javascript中输入文本

javascript - 使用 ng-show 启用我的 div

angularjs - 如何在 AngularJS 中连接相邻的隔离范围

javascript - 如果 Angular 中没有声明模块,则函数未定义