1

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); 
dmvianna
  • 15,088
  • 18
  • 77
  • 106

1 Answers1

0

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

  • Hm.. explain what? requestEmulate function return promise which will resolve in interval from 300ms to 1000ms (emulation of http request). Resolve returns random generated array with length between 1 to 50. And all works fine, hove no any errors with $digest loop. – Alexander Strochkov Aug 20 '15 at 06:36
  • Well, my array must be created empty, **outside** the loop, and then I must concatenate inside it. Your code is confusing me, because it features both a resolve and a return statement. I'm not sure what either does, as I'm used to resolve() functions. So far I have been unable to make it work in my own context. – dmvianna Aug 20 '15 at 06:53
  • Can you discribe whot behavier tou want to reach? You want to concat data from all requests? – Alexander Strochkov Aug 20 '15 at 06:58
  • Yes. Although I originally hoped I could stream each result through the pipe. But `resolve()` works like `return`, it breaks out from the loop. I wanted to keep receiving each result and processing it as it became available. – dmvianna Aug 20 '15 at 07:01
  • Fixed. I just had to add an `if` statement making the `resolve()` conditional on receiving the last page. Still, it would be nicer to have a pipe. – dmvianna Aug 20 '15 at 07:31
  • I update example and it steel works, can you share your code? – Alexander Strochkov Aug 20 '15 at 07:33