AngularJs对forEach的承诺 [英] AngularJs promise in forEach

查看:101
本文介绍了AngularJs对forEach的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从服务器获取数据的方法. 我在foreach中使用它,之后需要将他绑定到$ scope变量. 像这样:

I have method for getting data from server. And I use it in foreach, and after it need bind him to $scope variable. Like this:

var qualityMix = [];
var engagementMix = [];
angular.forEach(versions, function (versions) {
    qualityMix.push(qualityScoreByTimeWithAppVerPromise(versions.version));
    engagementMix.push(engagementByTimeWithAppVerPromise(versions.version));
});

$scope.qualityScoreByTimeMix = function () {
    return $timeout(function () {
        return $q.all(qualityMix).then(function (data) {
            return {series: data};
        });
    });
};

$scope.engagementTimeMix = function () {
    return $q.all(engagementMix).then(function (data) {
        return {series: data};
    });
};

qualityScoreByTimeWithAppVerPromise engagementByTimeWithAppVerPromise 是用于从服务器获取数据的功能.然后 $ scope.engagementTimeMix $ scope.qualityScoreByTimeMix 需要带有promise的返回函数(可以).

qualityScoreByTimeWithAppVerPromise and engagementByTimeWithAppVerPromise it is functions for getting data from server. Then $scope.engagementTimeMix and $scope.qualityScoreByTimeMix need return functions with promise (is okay).

此代码有效,但并非总是如此,有时我会捕获异常 $ scope.xxx不是函数.

This code working but not always, some times I catch exceptions $scope.xxx is not a function.

我不知道如何解决它.请帮帮我.非常感谢!

I don't know how to fix it. Help me please. Thanks a lot!

UPD 这是构建图表的代码.

UPD It is code for build charts.

<div class="section">
    <highchart id="mix_quality_score_by_time" type="area" data="qualityScoreByTimeMix"
               chart-style="qualityScoreMixChartStyle"></highchart>
</div>

我在其他页面中调用的指令如下:

And my directive I invoke in other page, like this:

<compare-versions id="compare_versions_panel" start-day="timeFilter.startDay()"></compare-versions>

$ scope.xxx不是函数 我的意思是我在chrome控制台中捕获消息是什么 $ scope.engagementTimeMix和$ scope.qualityScoreByTimeMix它不是函数

$scope.xxx is not a function I mean what I catch message in chrome console what $scope.engagementTimeMix and $scope.qualityScoreByTimeMix it not a function

推荐答案

当您已经有值时,可以使用ng-if并绘制图形
例如:

You can use ng-if and draw graph when you already have value
example:

<div class="section" ng-if="qualityScoreByTimeMix">
    <highchart id="mix_quality_score_by_time" type="area" data="qualityScoreByTimeMix" chart-style="qualityScoreMixChartStyle"></highchart>
</div>

这篇关于AngularJs对forEach的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆