サービスとコントローラを同じjsファイルに配置しました。だから私はサービスからデータを取得しようとし、私のhtmlでそれを使用しています。私のコードでは、サービスからデータを生成することはできますが、コントローラの$scope
に割り当てることはできず、htmlで使用できます。では、データを取得して$scope
に割り当てて、自分のhtmlで使用できるようにします。あなたはこのようなあなたのコードを書き換える場合コントローラ内のサービスデータを使用
var app = angular.module("app",[]);
app.factory('factoryServices',function($http){
var newObject = {};
var _getChart= function(){
$http.get("http://citibikenyc.com/stations/json")
.success(function(data, status){
if(data) {
return data;
}
}).error(function(data,status){
return error;
});
}
newObject.getChart = _getChart;
return newObject;
});
app.controller("chartController",function($scope,$http,factoryServices){
factoryServices.getChart($scope.chartServicesCompleted);
$scope.chartServicesCompleted = function(data){
$scope.serviceResponse = data;
}
})
すでに良い回答がありましたが、私もあなたのハンドリングのために.success()と.error()を使用しているという事実に触れたいと思います。約束する。 .success()と.error()は廃止され、1.6で完全に削除されたので、.then()に切り替えることを強くお勧めします。 –