如何从javascript + angularjs中的数组中获取值

时间:2017-01-18 11:30:43

标签: javascript angularjs arrays

我想从javascript + angularjs中获取数组索引的值,例如ng-repeat,但是在我的控制器中

Array[0]
  0: Object
     class:"text-area"
     text: "Arif#2:hi bro"
     time_stamp: 1484644646user_
     chat_img: "images/noimage.jpg"
    __proto__: Objectlength: 1__proto__: Array[0]

现在我要打印类名称" class = text-area"这将是一个循环 感谢

这是我的代码

 $scope.p=get_chat_history(); this function give me array
 console.log('key',$scope.p);

对不起,我很初学 我添加了更多细节

function get_chat_history() {
        var data = [];
        var time_stamp = '';
        $timeout(function () {
            $scope.time_stamp = window.localStorage.getItem('time_stamp');

        }, 3000);
        console.log('time_stamp', $scope.time_stamp);
        if (!$scope.time_stamp) {
            $scope.time_stamp = Date.now();
        }
        $http({
            url: "https://testing.twodegrees.io/chat/history/" + my_chat_id + "/" + my_chat_friend_id + "/" + $scope.time_stamp,
            headers: {
                'X-TWO_DEGREE-APP_ID': 'test_Anonymous_786',
                'X-TWO_DEGREE-APP_TOKEN': actoken
            },
            method: 'GET'
        }).then(function mySucces(success_res) {


            // console.log(' history data',success_res);
            var chat_images_images = [];
            for (var p = 0; p < success_res.data.chats.length; p++)
            {
                console.log('p', p);
                chat_images_images[p] = [];
                var message_body = success_res.data.chats[p].body;
                var myJSON = JSON.parse(message_body);
                var time_stamp = success_res.data.chats[p].time_stamp;
                var myJSON2 = JSON.parse(time_stamp);
                if (myJSON.sname == my_name) {
                    var class_name = 'text-area2';
                } else {
                    var class_name = 'text-area';
                }
                //console.log('time_stamp',myJSON2);
                if (p == 0) {
                    window.localStorage.setItem('time_stamp', myJSON2);
                }

                var newtest = {
                    text: myJSON.text,
                    class: class_name,
                    user_chat_img: "images/noimage.jpg",
                    time_stamp: myJSON2
                }
                // console.log('chat history',myJSON);
                data.push(newtest);

            }
            // console.log('chat history',data);

        });

        var urle = "https://testing.twodegrees.io/chat/history/" + my_chat_id + "/" + my_chat_friend_id + "/" + $scope.time_stamp;

        console.log('url history', urle);

        return data;

    }

这个函数发给我一个数组,我想用这个

2 个答案:

答案 0 :(得分:0)

修改

似乎你搞砸了异步调用并在解析http promise之前遍历数组。我在这里假设您的get_chat_history是异步(api)电话。

而不是$scope.p = get_chat_history()你应该做这样的事情:

get_chat_history().then(function(response) {
    $scope.p = response;
    // now you can iterate over the array, because the http promise is resolved
    // see below function to do that
});

如果我理解正确,你想迭代数组然后遍历它的所有值。您可以使用angular.forEach

来实现
// Iterate over array
angular.forEach($scope.p, function(item) {
    // Iterate over object keys
    angular.forEach(item, function(value, key) {
        console.log(key + "=" + value);
    });
});

答案 1 :(得分:0)

如果你想要简单,你可以像

那样做
for(var i=0; i<$scope.p.length; i++){
   console.log($scope.p[i].class);
   console.log($scope.p[i].text);
}

如果这在控制器中不起作用,您可以尝试

get_chat_history().then(function(res) {
    $scope.p = res;
    for(var i=0; i<$scope.p.length; i++){
        console.log($scope.p[i].class);
        console.log($scope.p[i].text);
    }
}

或者

get_chat_history().then(function(res) {
    for(var i=0; i<res.length; i++){
        console.log(res[i].class);
        console.log(res[i].text);
    }
}