Marcelo Johanes Julio Saerang Marcelo Johanes Julio Saerang - 11 months ago 46
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


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: