首页 > 代码库 > angularJs 之deferred

angularJs 之deferred

angularJs 对promise的封装

  var deferred = $q.defer();
    deferred.promise.then(function(){
        var result = {}
        cgiSvc.getGameList(result, function () {
            $scope.game_list = result.info;
        });
    },function(result){

    })
    $timeout(function(){
        deferred.resolve()
    },100)

 

then的灵活运用

                process_history:function(){
                    var deferred = $q.defer();    //在方法内声明
                    var request={};
                    request.op=‘process_history‘;
                    gs.post(‘alarm_adapter‘,request,function(ret){
                        $.each(ret.data,function(k,item){
                            item.name=item.content;
                            return item;
                        })
                        $scope.process=ret.data;
                        deferred.resolve();   // ajax成功返回后,执行 resolve操作
                        if(!$scope.$$phase) {
                            $scope.$apply();
                        }
                    })
                    return deferred.promise;//类似闭包 返回一个方法内的对象,或方法
                },

调用

    gs.process_history()
                    .then(function(){
                        gs.forbidden();
                    });

 

angularJs 之deferred