0

sorry, maybe a stupid problem, but I am still a newbie with angular.

The code:

-- EDIT #2 -- ----- ----- ----- ----- ----- ----- -----

MyApp.controller('DataController', ['$http', function ($http) {

        var self = this;

        var objects = [];
        var loaded = false;

        var getObjects = function () {
            if(!loaded){
                setRawObjects();
            }
            return objects;
        };

        var setRawObjects = function(){
            $http.get('data/objects.json').success(function (response) {
                loaded=true;
            });
        }

        var openDetail = function () {
            console.log('test');
        }

        self.getObjects = getObjects;
        self.openDetail = openDetail;
    }]);

-- END OF EDIT #2 -- ----- ----- ----- ----- ----- -----

The Error:

Error: [$rootScope:infdig] http://errors.angularjs.org/1.5.5/$rootScope/infdig?p0=10&p1=%5B%5D
    at angular.js:38
    at n.$digest (angular.js:17111)
    at n.$apply (angular.js:17337)
    at angular.js:1749
    at Object.invoke (angular.js:4665)
    at c (angular.js:1747)
    at yc (angular.js:1767)
    at ee (angular.js:1652)
    at angular.js:30863
    at HTMLDocument.b (angular.js:3166)

angular.js:38Uncaught Error: [$rootScope:infdig] http://errors.angularjs.org/1.5.5/$rootScope/infdig?p0=10&p1=%5B%5D
    at angular.js:38
    at n.$digest (angular.js:17111)
    at n.$apply (angular.js:17337)
    at angular.js:1749
    at Object.invoke (angular.js:4665)
    at c (angular.js:1747)
    at yc (angular.js:1767)
    at ee (angular.js:1652)
    at angular.js:30863
    at HTMLDocument.b (angular.js:3166)

Exception is thrown twice. I connot imagine what it means, whats wrong? This line seems to curse the problem:

return $http.get('data/objects.json').success(function(response) {

The controller is beeing initiated in this way:

EspanioApp.controller('DataController', ['$http', function ($http) {...

Do you have any hint for me how I can fix this?

cu n00n

1
  • Infinite $digest cycle... Commented Jan 4, 2017 at 1:36

1 Answer 1

2

This is the error:

$rootScope:infdig (Infinite $digest Loop)

This error occurs when the application's model becomes unstable and each $digest cycle triggers a state change and subsequent $digest cycle. Angular detects this situation and prevents an infinite loop from causing the browser to become unresponsive.

Probably setRawObjects is bound in a not so proper way.

Also, I'm not really sure what you do with what setRawObjects is returning: I'd verify what it is returning - probably a promise, which should be handled asynchronously, so a direct html binding wouldn't handle it properly.

For example, if you print the return on console, you'll find out that the object is undefined, while the response is not, which means that the return is executed before the success callback:

setRawObjects = function(){
    return $http.get('data/objects.json').success(function(response) {
        console.log(response);
        objects = response.objects;
        loaded = true;
    });
}

var getObjects = function () {
    console.log(loaded);
    if(!loaded){
        setRawObjects();
    }
    console.log(objects);
    return objects;
};

So, instead of binding the function, bind the object directly:

MyApp.controller('DataController', ['$http', function ($http) {
    var self = this;
    self.objects = [];

    $http.get('data/objects.json').success(function (response) {
        console.log(response);
        self.objects = response;
    });
}]);

Angular will watch it's been updated and will update the view as well.

Sign up to request clarification or add additional context in comments.

4 Comments

.then instead of .success also not works. .... Maybe EDIT #1 is the problem. Would like to load my json file once on loading the page.
@n00n I have updated my answer. I think one of the main problem is that you want to return synchronously the response of an asynchronous request.
I aggree with you, ... but the return was a fail cause I used a bad code example. Without return, I get the same behaviour if it gets deleted
@n00n I have added a possible solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.