Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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
Angularjs 为什么我需要$scope.$digest in Promise_Angularjs_Angularjs Digest - Fatal编程技术网

Angularjs 为什么我需要$scope.$digest in Promise

Angularjs 为什么我需要$scope.$digest in Promise,angularjs,angularjs-digest,Angularjs,Angularjs Digest,全部: 我对angular digest还很陌生,现在,当我在其then函数中使用Promise时,我必须使用$scope。$digest()使范围变量更改在其他地方生效,例如: 这里我使用一个承诺来模拟$http请求,我的困惑在于$scope.getdata,为什么我需要调用$scope。$digest(),我认为$scope.disable应该由angular自动监视。 var app = angular.module("vp", []); app .service("srh", f

全部:

我对angular digest还很陌生,现在,当我在其then函数中使用Promise时,我必须使用$scope。$digest()使范围变量更改在其他地方生效,例如:

这里我使用一个承诺来模拟$http请求,我的困惑在于$scope.getdata,为什么我需要调用$scope。$digest(),我认为$scope.disable应该由angular自动监视。

var app = angular.module("vp", []);
app
    .service("srh", function($http){
        var busy = false;
        this.get = function(url){
            if(!busy){
                busy = true;
                var p = new Promise(function(res, rej){
                            $timeout(function(){
                                res("data is fetched");
                            }, 3000);
                        })
                        .then(function(data){
                            busy = false;
                            return data;
                        }, function(data){
                            busy = false;
                            return data;
                        });
                return p;
            }else {
                return null;
            }
        }
    })// end of service
    .controller("main", function($scope, srh){
        $scope.disable = false;
        $scope.getdata = function(){
            var dp = srh.get("");
            if( dp ) {
                $scope.disable = true;
                dp.then(function(data){
                    console.log(data);
                    $scope.disable = false;
                    $scope.$digest()
                })
            }
        }
    })
用于内部处理所有摘要需求

每当您使用angular core之外的事件来修改范围时,您需要告诉angular,以便它可以更新view,该视图将在内部处理所有摘要需求


每当您使用angular core之外的事件来修改范围时,您需要告诉angular,以便它可以更新视图

谢谢,我对这一点很陌生,您能列出哪些情况可以归类为“事件”?第三方库事件、dom事件、本机承诺、本机计时器(如setTimeout)或类似postMessage的内容。任何不是angular API直接部分的内容请注意,
$http
使用
$q
来返回其承诺,我对这一点非常陌生,您能列出哪些情况可以归类为“事件”?第三方库事件、dom事件、本机承诺、本机计时器(如setTimeout)或类似postMessage的内容。任何不直接属于该协议的内容请注意,
$http
也使用
$q
来回报其承诺