Javascript $q在angularJs中的工作原理

Javascript $q在angularJs中的工作原理,javascript,jquery,angularjs,q,Javascript,Jquery,Angularjs,Q,我想知道$q在angular中是如何工作的,假设我有几行代码要执行 var app = angular.module('app', []); app.controller('HelloCtrl', function ($q, $scope) { $scope.Title = "Promise Demo"; function asyncGreetings(name) { var deferred = $q.defer(); setTimeou

我想知道$q在angular中是如何工作的,假设我有几行代码要执行

var app = angular.module('app', []);

app.controller('HelloCtrl', function ($q, $scope) {

    $scope.Title = "Promise Demo";

    function asyncGreetings(name) {

        var deferred = $q.defer();
        setTimeout(function() {

            deferred.notify('About to greet ' + name + '.');
            if (name == "binson") {
                deferred.resolve("Hello " + name);
            } else {
                deferred.reject("Greeting not allowed ");
            }

        }, 5000);

        return deferred.promise;
    };


    $scope.CurrentName = "";
    var promise = asyncGreetings('binson');

    promise.then(function(greeting) {
        $scope.CurrentName='Success: ' + greeting;
    }, function(reason) {
        $scope.CurrentName ='Failed: ' + reason;
    }, function (update) {

        $scope.Progress = 'Got notification: ' + update;
    });


 });

这是一个示例代码,这里是一个模拟异步函数asynchgreats(),将在5秒后执行,我想显示一个进度(一个简单的警报框或任何东西),直到收集到对调用方的承诺为止,我想我用3个参数(成功、失败、进度)正确配置了调用方函数。但是进度处理程序不起作用。。这里有什么问题,我如何解决这个问题。

进度处理程序不会自动更新

您必须以类似的方式使用
deferred.notify('message')
来解决/拒绝更新

在处理超时和间隔时,最好使用angular提供的$timeout和$interval服务来包装本机javascript函数。只需确保将它们包含在您的提供者中


不要使用
clearTimeout(timeout\u instance)
clearInterval(interval\u instance)
而使用
$timeout.cancel(timeout\u instance)
$interval.cancel(interval\u instance)
在同一个5秒钟的超时时间内有通知,因此它只会在5秒钟结束时触发

如果在不同的间隔循环中设置通知,则进度回调可以正常工作

   for (i = 0; i < 5; i++) {
      (function(i) {
        setTimeout(function() {
          deferred.notify('Notification #' + (i + 1));
        }, i * 1000);
      })(i);
    }

    setTimeout(function() {
      if (name == "binson") {
        deferred.resolve("Hello " + name);
      } else {
        deferred.reject("Greeting not allowed ");
      }

    }, 5000);
(i=0;i<5;i++)的
{
(职能(一){
setTimeout(函数(){
延期通知(“通知”+(i+1));
},i*1000);
})(i) );
}
setTimeout(函数(){
如果(名称=“binson”){
延迟。解决(“你好”+姓名);
}否则{
延期。拒绝(“不允许问候”);
}
}, 5000);

以上所有解决方案都很好地消除了我对q的疑虑,在这里,我有一个非常好的解决方案,适用于所有角度实用程序。我更改了异步函数,现在它使用$interval调用defer.notify()

function asyncGreetings(name) {

        var deferred = $q.defer();
        var progress = 0;

        var interval = $interval(function() {

            if (progress >= 100) {
                $interval.cancel(interval);
            }
            progress += 10;

            deferred.notify(progress);
        },1000);
        $timeout(function() {

          //  deferred.notify('About to greet ' + name + '.');
            if (name == "binson") {
                deferred.resolve("Hello " + name);
            } else {
                deferred.reject("Greeting not allowed ");
            }

        }, 10000);

        return deferred.promise;
    };
我在控制器中添加了一个名为$interval、$timeout的参数(依赖注入)


OP正在使用延迟。通知
app.controller('HelloCtrl', function ($q, $scope, $interval,$timeout) {///}