为什么函数不等到承诺得到解决?

时间:2016-03-15 03:18:03

标签: javascript angularjs angular-promise

我正在使用下面的代码从方法中返回字符串,如果http promise已经解析但是控件没有等到promise被返回并且我看到return对象是switch中的方法方法。有人可以告诉我是什么我在这里做错了!!

控制器具有以下逻辑:

this.postdata=function(data){
return $http.post(URL,DATA);
}

}

服务方式:

// JavaScript Document

;
jQuery(document).ready(function () {
    "use strict";
    jQuery(window).scroll(function () {
        var scrollTop = jQuery(window).scrollTop();
        var docHeight = jQuery(document).height();
        var winHeight = jQuery(window).height();
        var scrollPercent = (scrollTop) / (docHeight - winHeight);
        var scrollPercentRounded = Math.round(scrollPercent * 100);
        jQuery('#post_indicator').css('width', scrollPercentRounded + '%');
    });
    jQuery(window).scroll(function () {
        if (jQuery('#pp_fixed_menu').val() == 1)

        {
            if (jQuery(this).scrollTop() >= 200) {
                jQuery('.header_style_wrapper .above_top_bar').hide();
                jQuery('.extend_top_contact_info').hide();
                jQuery('.top_bar').addClass('scroll');
                jQuery('#post_info_bar').addClass('scroll');
            } else if (jQuery(this).scrollTop() < 200) {
                jQuery('.header_style_wrapper .above_top_bar').show();
                jQuery('.extend_top_contact_info').show();
                jQuery('#custom_logo img').removeClass('zoom');
                jQuery('#custom_logo img').css('maxHeight', '');
                jQuery('#custom_logo').css('marginTop', parseInt(logoMargin) + 'px');
                jQuery('#menu_wrapper div .nav > li > a').css('paddingTop', menuPaddingTop + 'px');
                jQuery('#menu_wrapper div .nav > li > a').css('paddingBottom', menuPaddingBottom + 'px');;
                jQuery('.top_bar').removeClass('scroll');
                jQuery('#post_info_bar').removeClass('scroll');
            }
        } else {
            if (jQuery(this).scrollTop() >= 200) {
                jQuery('.header_style_wrapper').addClass('nofixed');
            } else {
                jQuery('.header_style_wrapper').removeClass('nofixed');
            }
        }
    });

});

1 个答案:

答案 0 :(得分:1)

.success.error方法忽略返回值。

参数值区分大小写

this.postdata=function(data){
    //THIS
    return $http.post(URL,data);
    //return $http.post(URL,DATA);
}
  

弃用通知 1

     

已弃用$http遗留承诺方法.success.error。请改用标准.then方法。

$scope.submitdataPromise = function() {
    return service.postdata($scope.formdata);
});

$scope.submitdataPromise
  .then(function onFulfilled(response){
    $scope.data = response.data;
    //return data for chaining
    return data;
}).catch(function onRejected(response){
    console.log(response.status);
    //throw to chain error
    throw response;
});

submitdataPromise函数会立即返回承诺。承诺是XHR的 future 结果的容器,已解决或已完成或被拒绝。可以通过注册具有$q服务的函数来检索这些结果,以便在解析时调用。

  

响应对象具有以下属性:

     
      
  • data - {string|Object} - 使用转换函数转换的响应主体。
  •   
  • status - {number} - 响应的HTTP状态代码。
  •   
  • 标题 - {function([headerName])} - 标题获取功能。
  •   
  • config - {Object} - 用于生成请求的配置对象。
  •   
  • statusText - {string} - 响应的HTTP状态文本。
  •   
     

200到299之间的响应状态代码被视为成功状态,并将导致调用成功回调。请注意,如果响应是重定向,XMLHttpRequest将透明地跟随它,这意味着不会为此类响应调用错误回调。

- AngularJS $http Service API Reference - General Usage