繁体   English   中英

如何控制日志记录此对象

[英]How can I console log this object

Maby它只是监督它但我无法控制日志值

所以当我记录这个

console.log($scope.data)

比我回到页面上的控制台

$$state: Object
status: 1
    value: Array[11]
    0: Object
    1: Object
    2: Object
    3: Object
    4: Object
    5: Object
    6: Object
    7: Object
    8: Object
    9: Object
    10: Object

我只想说

console.log($scope.data.value[0]) 

给我一个价值中的第一个对象。 但是我只会尝试使用任何不确定的东西。 我错过了什么,我不能单独获得对象的价值。 Maby它是一个有角度的东西,因为$$但我现在有点无能为力。

这是我在控制台记录数据时获得的屏幕截图

更新发布我的代码如何获取我的数据:

(function () {
'use strict';
angular
    .module('testApp')
    .factory('storage', storage);

storage.$inject = ['$http'];

function storage($http) {
    return {
        getData: getData
    };

    function getData() {
        return $http.get('http://localhost:4040/')
            .then(complete)
            .catch(failed);

        function complete(response) {
            var merge = data.teams.map (function (data) {
                data.members = response.data.filter (function (person) {
                    return person.room == data.rooms
                });
                return data;
            });
            return merge;
        }

        function failed(error) {
            console.log('getting the data has failed' + error.data);
        }
    }
}
})();

服务

(function () {
'use strict';
angular
    .module('testApp')
    .service('testStorage', testStorage);

testStorage.$inject = ['storage'];

function testStorage(storage) {
    //Create empty variables


    //Data in variables
    var allData = storage.getData();

    //Return functions
    var service = {
        getAll: getAll
    };

    return service;

    ///////////////////////////
    // Create functions here //
    ///////////////////////////

    function getAll() {
        return allData;
    }

}
})();

并最终我的控制器

(function() {
'use strict';

angular
    .module('testApp')
    .controller('tryOutController', tryOutController);

tryOutController.$inject = ['$scope', 'testStorage'];

function tryOutController($scope, testStorage) {
    $scope.data = {};
    $scope.data = testStorage.getAll();

    console.log($scope.data);

}
})();

所以问题是你在填充之前试图访问你的数据。 使用适当的承诺功能:

function tryOutController($scope, testStorage) {
    $scope.data = {};
    testStorage.getAll().then(function(data) {
        $scope.data = data;
        console.log($scope.data);
    });
}

听起来,就像你在对象上面有值之前记录它一样。 在许多浏览器(如Chrome)上,控制台会记录对控件稍后展开的对象的引用。 这意味着,当你在控制台中展开对象后,您看到的值,因为他们的话 ,不是因为他们当你登录的对象。

因此,如果console.log($scope.data.value[0])显示undefined但是console.log($scope.data)显示了一个value数组,当您展开它时,它具有值,它意味着它在记录后填写。

但是 :看看你的第一个例子,我认为你应该看看$scope.data.$$state.value[0] ,而不是$scope.data.value[0]

下面的代码将“stringify”(转换为字符串表示形式)传递给它的任何对象。 因此,在您的情况下,您有一个对象数组,因此这将打印该数组中的第一个对象。

console.log(JSON.stringify($scope.data.value[0]));

您可以更进一步,使用for循环迭代数组并依次打印每个数组:

for(var i = 0; i < $scope.data.value.length; i++) {
   console.log(JSON.stringify($scope.data.value[0]));
   // For spacing...
   console.log('---------------------');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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