2017-03-17 9 views
0

工場のコントローラから結果を取得しようとしています。工場でコントローラにオブジェクトが返されていません

これまで私は失敗していました。すべてが工場内で問題なく、結果のconsole.logを作ることができ、うまく表示されます。

任意のポインタ?現時点では

私の工場:

mainApp.factory('statusFinder', ['jsonQueryStations', 'timeConverter', function(jsonQueryStations, timeConverter){ 
    var findTheTime = timeConverter.getTime(); 
    var generator = function(){ 
     return jsonQueryStations.stationData().then(function(result){ 
     if (result.station[findTheTime]>result.station.Average){ 
     return "Station is busy"; 
     } else{ 
     return "Station is quiet"; 
     } 
     }); 
    } 
    return { 
    status: function(){ 
     return generator(); 
    } 
    } 
}]) 

そして、現時点での私のコントローラはそうのようになります。

mainApp.controller('stuff', ['$scope', 'statusFinder', function($scope, statusFinder){ 
var data = statusFinder.status(); 
$scope.testing = data; 
}]) 

答えて

0

uは代わりに、工場のコントローラからの約束をキャッチすることができます。工場でのみjsonQueryStations.stationData()を返してください。コントローラで今

mainApp.factory('statusFinder', ['jsonQueryStations', 'timeConverter', function(jsonQueryStations, timeConverter){ 
    var generator = function(){ 
     return jsonQueryStations.stationData() 
    } 
    return { 
    status: function(){ 
     return generator(); 
    } 
    } 
}]) 

この

mainApp.controller('stuff', ['$scope', 'statusFinder', function($scope, statusFinder) { 
    var findTheTime = timeConverter.getTime(); 
    statusFinder.status().then(function(result) { 
     if (result.station[findTheTime] > result.station.Average) { 
      $scope.testing = "Station is busy"; 
     } else { 
      $scope.testing = "Station is quiet"; 
     } 
    }); 
}]) 
+0

のような約束をキャッチおかげで、私は私は、物事を整理するために、今のコントローラのうち、私の機能を移動しようとしています、以前に似た何かをしましたそれだけで工場でできることではありませんか? – Blimeys

+0

あなたの質問httpで約束を返すと約束を待つコントローラdosn't約束は、コントローラからキャッチします。これは、アンチパターンを避けるために、 –

+0

にも注意する適切な方法です。それは約束の中に別のカスタム約束を作成し、それは混乱を作ります –

関連する問題