Angular / 控制器加载 json 文件失败,出现异常我无法理解

Angular / Controller loading a json file fails with an exception I can not understand

抱歉,这可能是个愚蠢的问题,但我仍然是 angular 的新手。

代码:

-- 编辑#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;
    }]);

-- 编辑#2 结束-- ----- ----- ----- ----- ----- -----

错误:

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)

异常抛出两次。我无法想象这意味着什么,有什么问题吗? 这行好像是骂人的问题:

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

控制器正在以这种方式启动:

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

你有什么办法可以解决这个问题吗?

cu n00n

这是错误:

$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.

可能 setRawObjects 的绑定方式不太正确。

此外,我不太确定你对 setRawObjects 是什么 returning 做了什么:我会验证它是什么 returning - 可能是 promise,应该异步处理,因此直接 html 绑定无法正确处理它。

例如,如果你在控制台打印return,你会发现objectundefined,而response不是,这表示 return 在成功回调之前执行:

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;
};

所以,不是绑定函数,而是直接绑定对象:

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 会看到它已更新,也会更新视图。