を通過せずにGET呼び出して使用することはできます私のservice.jsである:ここでは
app.service('myService',['$http',function($http){
this.getSomething = function($scope){
return $http({
method: "GET",
url: "...",
headers: {...}
}).success(function(response){
$scope.items = response;
}).error(function(response){
...
});
}
}]);
は私controller.jsです:
app.controller('myController',['$scope','myService',function($scope,myService){
$scope.items = {};
myService.getSomething($scope);
}]);
しかし、私は疑問に思っては別がありますWebアプリを使用する方法は、サービス内の関数に '$ scope'を渡すことなく取得できますか?
...
this.getSomething = function(){
return $http({
...
}).success(function(response){
return response;
}).error ...
...
}
コントローラで::この(私はこれを試してみましたが、動作しない)のような例えば
...
$scope.items = myService.getSomething();
私は、あなたが 'success'関数の代わりに' then'を使うことをお勧めします。 http://blog.ninja-squad.com/2015/05/28/angularjs-promises/とhttps://www.peterbe.com/plog/promises-with-$httpをご覧ください –