简体   繁体   中英

Async ran during digest phase

I'm running this code in an Angular service, immediately upon loading the page. The controller $scope is passed as an argument to the function this extract belong to. The function is a $q promise.

I am not able to figure out how can I let the controller know that scope.req.rows has been updated. If I add scope.$apply() right after it, I run into a running digest phase. If I use the $q resolve function, it returns and no more loop results are returned. scope.$evalAsync() and $timeout seem to have no effect (at least without setting a timeout > 0). Same goes for scope.$watch .

How can I let the controller know that values were updated?

for (var page = 0; page < nbPages; page++) {
(function (pageNum) {

    that.get(url, 
             where, 
             res.pageSize * page, 
             res.pageSize)

        .then(function Success(data) {
        $log.info('Result of page ' + pageNum + ' received');

        for (row in data) {
                scope.req.rows++;
        }

    }).catch(function chunkFail(err) {
        reject(err);
    });

})(page); 

I build simple demo and it`s works. Correct me if i wrong.

Updated : i mocking http request and delay it form 1000ms to 30000ms. and i steel have't any scope problems. http://jsbin.com/wasoxe/4/edit?js,output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM