角度的承诺在没有显示的情况下

时间:2021-09-28 03:21:18

I have the following angular app, and the service is not returning the data properly. If I console.log the $http callback response, I know the data is good from the web service.

我有下面的角应用,服务没有正确返回数据。如果我控制台。记录$http回调响应,我知道来自web服务的数据很好。

However, in my app it is not displaying. Here is the HTML:

但是,在我的应用中它没有显示。HTML:

<div id="main-content" class="content" ng-app="wflow-app" ng-controller="main-controller">
                <h3>Actors</h3>
                <li>
                    <ul ng-repeat=" a in actors">
                        <li>{{a}}</li>
                    </ul>
                </li>

Here is the Javascript:

这是Javascript:

var app = angular.module('wflow-app', []);

app.factory('dataService', function($http, $q){
    var _baseUrl = webServiceContext;
    var deferred = $q.defer();

    var getActors = function(){
        $http.get(_baseUrl + '/actors').then(function(response){
            console.log(response.data);
            deferred.resolve(response.data);
        }).catch(function (response) {
            console.log("Rejected!");
            deferred.reject(response.statusCode);
    });
        return deferred.promise;
    };

    return {
        getActors: getActors
    };
});

app.controller('main-controller', function($scope, dataService, $timeout) {
    $scope.servicesUrl = webServiceContext;
    $scope.actors = dataService.getActors();
});

Why isn't this working?

为什么这不是工作吗?

If I use this in my controller, it works:

如果我在我的控制器中使用这个,它可以工作:

dataService.getActors().then(function (data) {
    $scope.actors = data;
});

1 个解决方案

#1


0  

where is your deferred.reject?

你的deferred.reject在哪里?

$http.get(_baseUrl + '/actors').then(function(response){
        console.log(response.data);
        deferred.resolve(response.data);
 }).catch(function(response) {
         deferred.reject(response.statusCode);
 });

if your server is responding with a non 200 response it will never get rejected at present

如果您的服务器正在响应一个非200的响应,目前它将永远不会被拒绝

i personally would just change this to ...

我个人想把这个改成……

var getActors = function(){
    return $http.get(_baseUrl + '/actors');
};

also the result of dataService.getActors() is a promise. Thus because the return value is a promise you should call the promises THEN method as follows ...

getactors()的结果也是一个承诺。因此,由于返回值是一个承诺,所以您应该调用promise然后按以下方法……

dataService.getActors().then(function(response) {
        $scope.actors=response.data;
}).catch(function(response) {
        //oops
})

This works because $http returns a promise. The only thing $q is doing is resolving with response.data instead of resolving with response.

这是因为$http返回一个承诺。$q做的唯一一件事就是用响应来解决问题。数据而不是响应解析。

#1


0  

where is your deferred.reject?

你的deferred.reject在哪里?

$http.get(_baseUrl + '/actors').then(function(response){
        console.log(response.data);
        deferred.resolve(response.data);
 }).catch(function(response) {
         deferred.reject(response.statusCode);
 });

if your server is responding with a non 200 response it will never get rejected at present

如果您的服务器正在响应一个非200的响应,目前它将永远不会被拒绝

i personally would just change this to ...

我个人想把这个改成……

var getActors = function(){
    return $http.get(_baseUrl + '/actors');
};

also the result of dataService.getActors() is a promise. Thus because the return value is a promise you should call the promises THEN method as follows ...

getactors()的结果也是一个承诺。因此,由于返回值是一个承诺,所以您应该调用promise然后按以下方法……

dataService.getActors().then(function(response) {
        $scope.actors=response.data;
}).catch(function(response) {
        //oops
})

This works because $http returns a promise. The only thing $q is doing is resolving with response.data instead of resolving with response.

这是因为$http返回一个承诺。$q做的唯一一件事就是用响应来解决问题。数据而不是响应解析。