程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在AngularJS 1.0.7中使用$ resources嵌套诺言大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在AngularJS 1.0.7中使用$ resources嵌套诺言?

开发过程中遇到在AngularJS 1.0.7中使用$ resources嵌套诺言的问题如何解决?下面主要结合日常开发的经验,给出你关于在AngularJS 1.0.7中使用$ resources嵌套诺言的解决方法建议,希望对你解决在AngularJS 1.0.7中使用$ resources嵌套诺言有所启发或帮助;

您可以$promise从函数中的BoatType资源返回资源,parseBoatTime并使用Promise来解决parseUrl延迟的问题。

首先从parseBoatTime函数返回一个promise :

return BoatType.getBoatTypeByname({
    name: boatTypeParsed
  }, function success(result) {
    return result;
  }).$promise;

然后parseUrl deferredBoatType资源中的承诺解决:

parseBoatType().then(deferred.resolve);

贝娄是从您的问题中得到的完整代码,我提到了更正。

var parseURL = function() {

  var deferred = $q.defer();

  var promise = deferred.promise;

  promise.then(function success(result) {

    console.log(result);

    searchBoats(result);

  });



  parseBoatType().then(deferred.resolve);

};

parseURL();



var parseBoatType = function() {



  //  Do some stuff

  // Calculate boatType calling a service that uses resource to call

  // an API

  // I can convert this callback into a promise but still facing same

  // issue



  // Code for ngResource@^1.2.0

  /*return BoatType.getBoatTypeByname({

    name: boatTypeParsed

  }, function success(result) {

    return result;

  }).$promise;*/



  // Code for ngResource lower than 1.2.0

  var deferred = $q.defer(), promise = deferred.promise;



  BoatType.getBoatTypeByname({

    name: boatTypeParsed

  }, deferred.resolve, deferred.reject);



  return promise;



  // The service method is called and the code is still running until

  // the end of the function without waiting for the service result.

  // Then the promise.then code in the parseURL is executed and

  // searchBoats is run with boatType undefined.

};



// The service with the $resource call to the API

app.factory('BoatType',

  function($resource, SERVER_URL) {

    var boatTypes =

      $resource('http://' + SERVER_URL + '/:action', {

        action: 'boat_types'

      }, {

        query: {

          method: 'GET',

          isArray: true

        },

        getBoatTypeByname: {

          method: 'GET',

          params: {

            action: 'getBoatTypeByname'

          },

          isArray: false

        }

      });

    return boatTypes;

  }

)

解决方法

我需要在AngularJS 1.0.7中使用参数运行函数 “ searchBoats(boatType)” 。此参数是另一个函数
parseBoatType 的结果,该函数 正在运行使用$ resource调用API的服务
。当带有诺言的资源中返回boatType时,如何运行searchBoats?

这是我尝试的:

    var parseURL = function() {                                                         
            var deferred = $q.defer();
            var promise = deferred.promise;
            promise.then(function success (result) {                    
                console.log(result);
                searchBoats(result);
            });

            deferred.resolve(
                parseBoatType()
            );                                                                                                                                                                                                                          
        };      
        parseURL();

var parseBoatType = function() {

        //  Do some stuff        
        // Calculate boatType calling a service that uses resource to call 
        // an API
        // I can convert this callback into a promise but still facing same 
        // issue
        BoatType.getBoatTypeByName({name: boatTypeParsed},function success(result) {
                return result;
            });

        // The service method is called and the code is still running until 
        // the end of the function without waiting for the service result.
        // Then the promise.then code in the parseURL is executed and 
        // searchBoats is run with boatType undefined.                                  
    };

 // The service with the $resource call to the API
.factory('BoatType',function($resource,SERVER_URL){          
    var boatTypes =
     $resource('http://' + SERVER_URL +'/:action',{action:'boat_types'},{       
        query: {method:'GET',isArray: true},getBoatTypeByName: {method:'GET',params:{action: 'getBoatTypeByName'},isArray: false}
     });        
     return boatTypes;           
  }
  )

大佬总结

以上是大佬教程为你收集整理的在AngularJS 1.0.7中使用$ resources嵌套诺言全部内容,希望文章能够帮你解决在AngularJS 1.0.7中使用$ resources嵌套诺言所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: