4

我无法将我的大脑包裹在异步请求的概念上。

我的视图有一个控制器,它从提供者创建一个对象实例:

va.controller('VaCtrl',function($scope,$shipment){
    $scope.shipment = $shipment.Shipment();    
});

提供者:

Shipment.provider('$shipment',function(){

    this.$get = function($http){

        function Shipment(){

        }

        Shipment.prototype.fetchShipment = function(){
            var shipment = undefined;
                $http.post('../sys/core/fetchShipment.php',{
                        // some data to POST
            }).then(function(promise){
                    shipment = promise.data;
                });

            return shipment;
        };

        return {
            Shipment: function(){
                return new Shipment();
            }
        }
    }
});

我的目标是从Shipment.prototype.fetchShipment()我的控制器内部访问数据。我的做法:

$scope.fetchShipment = function(){
       var shipment = $scope.shipment.fetchShipment();
        console.log(shipment); // undefined
};

但是,这将返回未定义。

我读到了 $q,以及延迟、承诺和回调,现在我就像 WTF;我想做的就是将检索到的数据推送到我的控制器,最好的方法是什么?

4

2 回答 2

5

您应该如下所示修改您的代码以直接从 fetchshipment 返回承诺,然后在控制器中使用 then()。

Shipment.prototype.fetchShipment = function(){

    return $http.post('../sys/core/fetchShipment.php',{
        // some data to POST
    })
};


$scope.fetchShipment = function(){
    var shipment = $scope.shipment.fetchShipment().then(function(data){;
        console.log(data);
    });
};

代码说明:

调用 $http 返回一个承诺,当您从服务器获取数据时该承诺将被解决。在上面的代码中,我从返回承诺的服务函数返回了 $http.post。因此,在控制器中,您正在等待 promise 被解析,并且当 promise 被解析时,结果将记录到控制台。

阅读有关 Angular 的更多承诺文档:

于 2013-07-23T09:27:20.950 回答
4

只是给你一个例子,如何让你的例子与你自己的承诺一起工作。如果你使用 $http 内置 promise 会更简单,所以这是一个 $q 示例:

angular.module('myApp', []).controller("myAppCtrl", function ($scope, $shipment) {
    $shipment.Shipment().fetchShipment().then(function (shipment) {
        $scope.shipment = shipment
    });
}).provider('$shipment', function () {
    this.$get = function ($http, $q) {

        function Shipment() {

        }

        Shipment.prototype.fetchShipment = function () {
            var defered = $q.defer();
            demodata = {name: "jan", id:8282};
            $http.post('/echo/json/', 'json=' + encodeURIComponent(angular.toJson(demodata)), {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                }
            }).then(function (response) {
                //resolve promise
                defered.resolve(response.data);
            });

            return defered.promise;
        };

        return {
            Shipment: function () {
                return new Shipment();
            }
        }
    }
});

<div ng-controller="myAppCtrl">{{shipment}}</div>

JSFiddle(使用 JSFiddle echo-service 作为数据提供者):http: //jsfiddle.net/alfrescian/ayke2/

更多关于承诺:

http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/ http://www.egghead.io/video/o84ryzNp36Q AngularJS:在哪里使用承诺? stackoverflow.com/questions/15604196/... egghead.io/video/o84ryzNp36Q

于 2013-07-23T10:18:19.297 回答