Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在回调函数中正确使用承诺_Javascript_Angularjs_Callback_Angular Ui Router_Angular Promise - Fatal编程技术网

Javascript 如何在回调函数中正确使用承诺

Javascript 如何在回调函数中正确使用承诺,javascript,angularjs,callback,angular-ui-router,angular-promise,Javascript,Angularjs,Callback,Angular Ui Router,Angular Promise,我使用此服务,它使用回调异步返回学生列表: studentModule.factory('StudentService', function (DB_URL) { return { getAllStudents: function (callback) { var Datastore = require('nedb'), path = require('path');

我使用此服务,它使用回调异步返回学生列表:

    studentModule.factory('StudentService', function (DB_URL) {
        return {
            getAllStudents: function (callback) {
                var Datastore = require('nedb'),
                    path = require('path');
                db = {};
                db.students = new Datastore({
                    filename: DB_URL + '/students.db',
                    autoload: true
                });
                db.students.find({}, function (err, stds) {
                    callback(stds);
                });
            }; //end return 
我在控制器中使用它的旧方法:

StudentService.getAllStudents(function(sts) {
    $scope.students = sts;
    $scope.$apply();//notify angular about the change
});
paymentModule.controller('PaymentController', function($scope,students) {
    alert(JSON.stringify(students));
这对我很有用,但现在我想使用一些最佳实践。在到达控制器之前,我需要解析路由中的结果,这里是我所做的:

.state('payment', {
    url: '/payment',
    templateUrl: 'frontend/components/payment/views/payment.html',
    controller: 'PaymentController',
    resolve: {
        students: function (StudentService, $q) {
            var defer = $q.defer();
            defer.promise.then(function () {
                StudentService.getAllStudents(function (sts) {
                    alert(JSON.stringify(sts));
                    return sts;
                });
            })
            defer.resolve();
        }
    }
})
警报正在成功地从路由返回数据,但没有从控制器返回数据-我在控制器中得到一个未定义的

StudentService.getAllStudents(function(sts) {
    $scope.students = sts;
    $scope.$apply();//notify angular about the change
});
paymentModule.controller('PaymentController', function($scope,students) {
    alert(JSON.stringify(students));

任何帮助都将不胜感激

您应该始终返回解析函数的承诺,并且在创建自己的承诺时,您应该使用需要传递的数据解析它

.state('payment', {
    url: '/payment',
    templateUrl: 'frontend/components/payment/views/payment.html',
    controller: 'PaymentController',
    resolve: {
        students: function (StudentService, $q) {
            var defer = $q.defer();
            //defer.promise.then(function () {
            StudentService.getAllStudents(function (sts) {
                //alert(JSON.stringify(sts));
                //return sts;
                defer.resolve(sts);
            });
            //})
            //defer.resolve();
            return defer.promise
        }
    }
})

你没有回复你的承诺。你也没有用数据来解决你的承诺,你什么都没有解决。谢谢,我会检查它,我是新来的承诺请容忍我。你也可以在我检查代码时回答这个问题。我本来会让服务返回一个承诺,但这两个选项都有效。是的,它会更干净。这对我来说很省时谢谢凯文!