AngularJs causing infinite loop when there is a promise insite an filter.

1,252 views
Skip to first unread message

Ismael Kafeltz

unread,
Jan 28, 2014, 4:09:38 PM1/28/14
to ang...@googlegroups.com
The following code is spam me thousands repeated $http request until the browser stop working.

My only clue is something related to $digest.

I'm using v1.2.4.

// filter.js
(function (ng, app) 
{
    "use strict";

    app.filter("Cidade", ["cidadeService", "$log", "$http", function (cidadeService, $log, $http) 
    {
        return function (input) 
        {
            var promise = $http
            ({
                method: "GET",
                cache : true,
                url: window.API_HOST + "/api/Cidades/" + input
            });

            return 'test';
        };
    }]);

})(window.angular, window.app)

// some index.html
<p>Cidade: {{ instituicao.Endereco.Cidade.Id | Cidade }}</p>

This is very simple to reproduce.

ps: i know this is not a good practice to put a service inside a filter, does not make sense, it's like a view to know businesses rules from controller. But this code is quick & dirty solution will soon be refactored. (and this code is not originated mine)


Tony pee

unread,
Jan 28, 2014, 5:13:56 PM1/28/14
to ang...@googlegroups.com
I think you've answered your own question, when saying that you shouldn't have the $http request in the filter. 

The watching (result of the digest) works by comparing values, and often checks them multiple times, depending on whether it things things have 'settled'. If you spawn off a http request each time it is checked, then yes, you will cause a lot of requests.  

You should be only watching values which exist, or WILL exist when the promise returns. Maybe just set a variable for whether your request has been sent or not. At worst, give it a timeout, to throttle the 'updates' if you want it periodically updating. 


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.



--
Tony Polinelli

Ismael Kafeltz

unread,
Jan 29, 2014, 5:14:52 AM1/29/14
to ang...@googlegroups.com
I know I answered my own question.
But see this like a "bug-report". 
=D

Owen M

unread,
Jan 29, 2014, 3:39:58 PM1/29/14
to ang...@googlegroups.com
filters are run a LOT. Every time the scope changes, it fires digest cycles which calls the appropriate filters, often multiple times. Filters should always return the same output for a given input. In your case firing off http requests is causing all kinds of changes. Putting anything beyond basic logic in a filter is one of the best ways to slow down your application.

The http call should be put into a service to return what you are looking for. Use a controller to hook the service up to your template for display.
Reply all
Reply to author
Forward
0 new messages