简体   繁体   English

then(函数)没有在angularjs中用返回的promise值更新

[英]then(function) not getting updated with returned promise value in angularjs

I am trying to to get some data from API with the help of promises in angularjs. 我试图借助angularjs中的promises从API获取一些数据。 I have one service which is getting a value from API and returning the promise to controller. 我有一项服务是从API获取价值并将承诺返回给控制器。

***Service***
this.forgotPasswordLink = function(email){
    var deferred = $q.defer();
    $http.post('/forgotPassword',{
        email: email 
    }).success(function(response){
        console.log("the new response is: ",response);
        deferred.resolve(response);
    }).error(function(error){
        deferred.reject(error);
    });
    return deferred.promise;
};

**Controller**
authService.forgotPasswordLink($scope.forgotPasswordEmail).then(function(data){
        console.log(data);
        if(data.message === 'emailFound'){
            $scope.emailSent = true;
            $scope.emailNotSent = false;
        }
        else{
            $scope.emailNotSent = true;
            $scope.emailSent = false;
        }
    });

Here authservice is the name of the service . 此处authservice是服务的名称。 so this code is working fine but the problem is when I try to get data multiple times one after the other, for the first time promise is returning the correct data but when I try to post with different data, promise doesn't updates the then(function) even though promise gets proper response from server. 所以这段代码可以正常工作,但是问题是当我一次又一次尝试多次获取数据时,第一次保证会返回正确的数据,但是当我尝试使用不同的数据进行发布时,保证不会更新(功能)即使Promise从服务器获得了正确的响应。

if you see there are two console statements, one in service and one in controller; 如果看到两个控制台语句,一个在服务中,一个在控制器中; so the console statement in controller is getting executed first as promise is returning the old value and when promise is resolving, second console statement which is in service is executed with the updated value. 因此,当promise返回旧值时,首先执行控制器中的console语句,当promise解析时,将使用更新后的值执行正在使用的第二个console语句。

So how do I get the updated value in then(function). 因此,如何在then(function)中获取更新的值。 do I need to refresh the service after posting the data once. 发布数据一次后是否需要刷新服务?

As @Terry suggested, you don't have to depend on $q at all, to achieve the result. 正如@Terry所建议的那样,您完全不必依赖$ q来获得结果。 I recommend one of the following two approaches: 我推荐以下两种方法之一:

#1: #1:

***Service***
this.forgotPasswordLink = function(email){
    return $http.post('/forgotPassword',{
        email: email 
    }).then(function(response){
        console.log("the new response is: ",response);
        return response;
    }).catch(function(error){
        return error;
    });
};

In this case, the controller remains the same; 在这种情况下,控制器保持不变。 only thing to note is that, even in error case, the controller's then function will be called with data being the error object returning from catch block of service. 唯一需要注意的是,即使在错误情况下,也将调用控制器的then函数, data是从服务的catch块返回的错误对象。

#2: #2:

***Service***
this.forgotPasswordLink = function(email){
    return $http.post('/forgotPassword',{
        email: email 
    });
};

***Controller***
authService.forgotPasswordLink().then(function(data){
    console.log(data);
    if(data.message === 'emailFound'){
        $scope.emailSent = true;
        $scope.emailNotSent = false;
    }
    else{
        $scope.emailNotSent = true;
        $scope.emailSent = false;
    }
}).catch(function(error){
    console.log(error); // Error handling like notifying user, etc...
});

In the second approach, the service just returns the promise returned by $http . 在第二种方法中,服务仅返回$http返回的promise。 The controller then takes actions based on the result of this promise. 然后,控制器根据此承诺的结果采取措施。

One can argue that it's the service's duty to obtain the data and give it to controller to take actions. 有人可以说,获取数据并将其交给控制器采取行动是服务的职责。 This is the first approach. 这是第一种方法。

I personally prefer the first approach, since the service is simple and it's easy to test this way. 我个人更喜欢第一种方法,因为该服务很简单,并且以这种方式进行测试很容易。

当您使用then方法时,您已将.data放在变量之后,将data.data放在后面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM