AngularJS Question

how to make dynamic scope iteration in ng-repeat

i have scope like this in my controller

$scope.chats1 ={user_name:item.user_name,user_photo:item.user_photo}
$scope.chats2 ={user_name:item.user_name,user_photo:item.user_photo}
$scope.chats3 ={user_name:item.user_name,user_photo:item.user_photo}

And this is my currently ng-repeat:

<li ng-repeat="x in chats1"></li>
<li ng-repeat="x in chats2"></li>
<li ng-repeat="x in chats3"></li>

My question is how to make single ng-repeat that contains all of my scope like this

<li ng-repeat="x in chats"> /* defines $scope.chats1,$scope.chats2,$scope.chats3 */

Thank You and i'm really appreciate ur help

Answer Source

Its simple just push your scope objects in an array, call it chats.And repeat on it. The whole code would be:

<!DOCTYPE html>
<html ng-app="hcApp">

    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src=""></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>

  <body ng-controller = "hcAppCtrl">
    chats: {{chats}}
    <div ng-repeat="data in chats">{{data.user_name}}</div>

.controller('hcAppCtrl', ['$scope',function($scope){

  $scope.chats1 ={user_name:"alex",user_photo:"awesome"}
  $scope.chats2 ={user_name:"rumba",user_photo:"damn"}
  $scope.chats3 ={user_name:"sunil",user_photo:"hero"}
  var temp = [];


  $scope.chats =temp;

Here is the working plunker:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download