2017-01-18 17 views
0

我想在javascript中的數組索引值+ angularjs像NG-重複,但在我的控制器如何從在JavaScript數組提取值+ angularjs

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] 

現在我想打印類的名稱,如「類=文本區域」,這將是一個循環 感謝

這裏是我的代碼

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

對不起FO的一切,我感到非常初學者 我添加一些莫再詳細

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; 

    } 

這個功能給我一個數組,我想使用這個

+0

你可以發表y我們的代碼在這裏,或者一個jsfiddle/jsbin等的鏈接? – rrd

+0

提供給我們更多的細節....就像你想實現的......用你的嘗試 – Kenny

+0

我在我的視圖中使用循環ng-repeat得到了這個,但我現在想在我的視圖中獲得這個在我的控制器中,這ng-repeat =「mymessages消息」,然後{{message.class}} –

回答

0

編輯

好像你亂用異步調用,並遍歷HTTP承諾之前在陣列上已解決。我假設您的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); 
    }); 
}); 
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); 
    } 
} 
+0

這是在我的視圖中使用ng-repeat,但沒有在我的控制器中獲取這個 –

+0

@NazarHussain我可以做一個有教養的猜測,你的'get_chat_history()'是一個異步函數? – devqon