AngularJS – 访问http头

前端之家收集整理的这篇文章主要介绍了AngularJS – 访问http头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试访问我的角度控制器中的http标头,但我不明确.此外,我可以看到我的角度服务中的标题响应,这不反映在我的控制器中.有人可以告诉我我失踪了什么?请看下面的代码

服务:

cmApp.service('supplierService',function ($http,$q) {
    this.getSuppliers = function (orderByColumn,skipRows,takeRows) {
        var deferred = $q.defer();
        $http({
            method: 'GET',url: 'api/supplier',params: { orderBy: orderByColumn,skip: skipRows,take: takeRows },timeout: 30000,cache: false
        }).
        success(function (data,status,headers,config) {
            // any required additional processing here            
            deferred.resolve(data,config);            
        }).
        error(function (data,status) {
            deferred.reject(data,config);
        });
        return deferred.promise;        
    }

控制器:

supplierService.getSuppliers($scope.orderby,$scope.skip,$scope.take)
        .then(function (data,config) {
            **//getting undefined here.**
            $scope.totalRecords = parseInt(headers('X-TotalRowCount'));                
            $scope.suppliers = data;
        },function (error) {
            // error handling here
        });
我自己找到了解决方案.我所要做的就是创建一个数组,并将所有这些值添加到同一个&将其返回到控制器.请看下面更新的代码

服务:

cmApp.service('supplierService',config) {
            // any required additional processing here 
            var results = [];
            results.data = data;
            results.headers = headers();
            results.status = status;
            results.config = config;

            deferred.resolve(results);            
        }).
        error(function (data,$scope.take)
            .then(function (response) {                
                $scope.suppliers = response.data;
                $scope.totalRecords = parseInt(response.headers["x-totalrowcount"]);                
            },function (error) {
                // error handling here
            });
原文链接:https://www.f2er.com/angularjs/142883.html

猜你在找的Angularjs相关文章