AngularJs 在 forEach 中的承诺 [英] AngularJs promise in forEach

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

问题描述

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

var qualityMix = [];varengageMix = [];angular.forEach(版本,功能(版本){qualityMix.push(qualityScoreByTimeWithAppVerPromise(versions.version));engagementMix.push(engagementByTimeWithAppVerPromise(versions.version));});$scope.qualityScoreByTimeMix = 函数 () {返回 $timeout(function () {返回 $q.all(qualityMix).then(function (data) {返回{系列:数据};});});};$scope.engagementTimeMix = 函数 () {返回 $q.all(engagementMix).then(function (data) {返回{系列:数据};});};

qualityScoreByTimeWithAppVerPromiseengagementByTimeWithAppVerPromise 是从服务器获取数据的函数.然后 $scope.engagementTimeMix$scope.qualityScoreByTimeMix 需要返回带有 promise 的函数(没问题).

此代码有效但并非总是如此,有时我会捕获异常$scope.xxx is not a function.

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

UPD它是构建图表的代码.

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

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

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

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

解决方案

当你已经有了价值时,你可以使用 ng-if 并绘制图形
例子:

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

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 and engagementByTimeWithAppVerPromise it is functions for getting data from server. Then $scope.engagementTimeMix and $scope.qualityScoreByTimeMix need return functions with promise (is okay).

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 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 is not a function I mean what I catch message in chrome console what $scope.engagementTimeMix and $scope.qualityScoreByTimeMix it not a function

解决方案

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天全站免登陆