如何在javascript+;安格拉斯

如何在javascript+;安格拉斯,javascript,angularjs,arrays,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] 现在我想

我想从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;

    }
函数get\u chat\u history(){
var数据=[];
var时间戳=“”;
$timeout(函数(){
$scope.time_stamp=window.localStorage.getItem('time_stamp');
}, 3000);
log('time\u stamp',$scope.time\u stamp);
如果(!$scope.time\u戳记){
$scope.time_stamp=Date.now();
}
$http({
url:“https://testing.twodegrees.io/chat/history/“+my_chat_id+”/“+my_chat_friend_id+”/“+$scope.time_stamp,
标题:{
“X-TWO_DEGREE-APP_ID”:“test_Anonymous_786”,
“X-TWO_DEGREE-APP_令牌”:actoken
},
方法:“获取”
}).然后(函数mySucces(success_res){
//console.log(‘历史数据’,成功率);
var chat_images_images=[];
对于(var p=0;p
此函数向我发送一个数组,我想使用此

编辑

看起来您搞乱了异步调用,在解析http承诺之前遍历了数组。这里我假设您的
get\u chat\u history
是一个异步(api)调用

您可能应该执行以下操作,而不是
$scope.p=get\u chat\u 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
});

如果我理解正确,您希望迭代数组,然后遍历它的所有值。您可以使用以下方法进行此操作:


如果你想要简单,你可以像这样做

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

for(var i=0;我能在这里发布你的代码,或者链接到jsfiddle/jsbin等吗?提供更多细节…比如你想实现什么…用你的tries在我的视图中使用循环ng repeat得到这个,但我现在想在我的视图中我的控制器中得到这个ng repeat=“我的消息中的消息”然后{message.class}你想在你的控制器中得到什么?如果你在这里提供一段代码,我们可以更好地帮助你。在我的视图中使用ng repeat是可行的,但在我的视图中没有controller@NazarHussain我可以有根据地猜测您的
get\u chat\u history()
是一个异步函数?
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);
    }
}