AngularJS Defer.promise未正常工作 [英] AngularJS Defer.promise not working as expected

查看:116
本文介绍了AngularJS Defer.promise未正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用的 AngularJS &放的应用; PersistenceJS

I'm developing an application using AngularJS & PersistenceJS.

我得到处理异步调用的麻烦作为

I'm getting trouble dealing with Asynchronous calls as the

控制器

cars.controller('CrashWidgetOneCtrl',function($scope, $location, $routeParams, CrashServices){  
    if($routeParams.crashId){
        $scope.data = {};
        console.log("CrashID: "+$routeParams.crashId);
        crashId = $routeParams.crashId; 
        alert(1);//Works

        CrashServices.getCrashDetails($scope, crashId).then(function(result){
            console.log(result);
            alert(2);//Never Fires
        });    
    alert(3);//Gets executed
    }else{
        console.log("N");
    }   

});

服务

cars.factory('CrashServices', function($http, $location, $q, CommonServices,$rootScope, $timeout){
    return{
        getCrashDetails:function($scope, crashId){
        var deferred = $q.defer();          
        // Get user details if any
        $scope.$apply(function(){
            var crashInfoTable = App.CrashInfoTable.all();
            alert(4);
            crashInfoTable.list(null, function (results) { 
                alert(5);//This also doesn't work
                deferred.resolve();
            }); 
        });
        return deferred.promise;
        }
    }

});

任何帮助将大大AP preciated。非常感谢。

Any help will be greatly appreciated. Many thanks.

请注意:我使用PersistenceJS

Note: I am using PersistenceJS.

推荐答案

我不知道你是否需要scope.apply。
这似乎为我工作:

I dont know if you need the scope.apply. This seemed to work for me:

getCrashDetails:function($scope, crashId){
    var deferred = $q.defer();          
    // Get user details if any
    App.CrashInfoTable.all().list(null, function(results) { 
        deferred.resolve(results);
    });
    return deferred.promise;
}

这篇关于AngularJS Defer.promise未正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆