2017-04-05 4 views
0

で動作していないに渡される "タイプ" パラメータは、形式で["hourAverage","hourMedian","dayAverage","dayMedian","weekAverage","weekMedian","monthAverage","monthMedian"]がangularjs

ソースです:

$scope.buildCharts = function(types){ 
    var deferred = $q.defer(); 
    var promises = []; 

    types.forEach(function(type){ 
       $scope.prepareData(type); 
       deferred.resolve([ 
       { 
        title: type, 
        curveType: 'function', 
        legend: { position: 'bottom' } 
       },type]); 
       promises.push(deferred.promise); 
    }); 
    return $q.all(promises); 
}; 

出力:

[ 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ], 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ], 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ], 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ], 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ], 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ], 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ], 
    [ 
    { 
     "title": "hourAverage", 
     "curveType": "function", 
     "legend": { 
     "position": "bottom" 
     } 
    }, 
    "hourAverage" 
    ] 
] 

答えて

2

これは、ことを示しています約束は1つの値でしか解決できません。同じ遅延オブジェクト上のresolveのそれ以上のコールは効果を持ちません。だからこそ、最初のオブジェクトを何度も何度も何度も何度も価値あるものにしています。これはPromises/A+ specifications, point 2.1.2(強調鉱山)での動作を必要とする:

満たさ

2.1.2、約束:          2.1.2.1

が他の状態に遷移してはいけません。

          2.1.2.2を変更してはならない値、を持っている必要があります。もちろん

それ約束の値を変更することが許されていた場合、あなたはまだ、望ましい結果を持っていないだろう最後、などの結果はの繰り返しを持っているだろうオブジェクト値。

移動この:

var deferred = $q.defer(); 

forEachコールバックの中へ:

あなたは常に新しい約束を解決するように、この問題を解決するには

、ループ以内に新しい繰延オブジェクトを作成

types.forEach(function(type){ var deferred = $q.defer(); // etc...