我的服务电话有问题。我想将所有获取请求放在一个单独的服务文件中。它正在工作,我在 .then
函数中的 Angular Controller 中接收数据。但我不知道如何将数据从函数中取出到我的范围内。函数中的 Console.log
向我显示了对象。 Console.log
外部函数向我展示了全部 promise 。我怎样才能把它弄到外面?我究竟做错了什么?
我的Service.js
:
app.service('mamiService', ['$http','toaster', '$q', '$filter', function( $http, toaster, $q, $filter) {
/GET THE EMAIL TEMPLATES
this.getTexts = function () {
return $http.get('includes/php/getMongoTexts.php')
.then(function(response) {
return response;
});
};
}]);
我在 Controller 中的功能:
var receivedData = mamiService.getTexts().then(function(response){
var newData = response.data;
console.log(newData);
return newData;
});
console.log(receivedData);
console(newData)
向我展示了包含所有数据的对象。
console(receivedData)
整个 promise 对象。我只想将数据返回到 $scope
中。
感谢您的帮助!
最佳答案
您走在正确的轨道上。只需确保将数据附加到 .then
mamiService.getTexts().then(function(response){
$scope.receivedData = response.data;
...
关于javascript - 从单独的服务文件中从 $http 获取数据到 $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33960589/