0

I have this code in angular

var divCounter = 1;
$scope.data = [];
$scope.data = data;  

angular.forEach($scope.data, function(value, key) {                                
   if (value.hasCar === 0 && value.id != 45){                                                                          
      var divtxt = "divtxt" + divCounter;    
       $scope.divtxt = value.description;
      divCounter++;         }
});

HTML is

<div > {{ divtxt1 }} </div>
<div > {{ divtxt2 }} </div>    

$scope.data is an array that contains JSON ,that came from ajax.

Loop through that table and grab only the objects that have no car and not an id of 45. So right now I have 2 objects out of 10. I know I will only get 2 objects and always put them in 2 divs.

Now , grab those objects and put them in the scope, that its name is created dynamically, divtxt1 or divtxt2.

This does not work, I get no errors in the console, but I also see nothing in my HTML.

What am I missing?

Thanks.

slevin
  • 4,166
  • 20
  • 69
  • 129
  • 1
    **Use an array!** The syntax is minimally different, but it's infinitely saner: `$scope.divtxt.push(..); divtxt[0];`... – deceze Mar 02 '16 at 13:29
  • What do you want to achieve that will help us to solve your problem efficiently? – Sanjay Sahani Mar 02 '16 at 13:30
  • @deceze Do you want to give me an example, so I can see what you suggest? Thanks – slevin Mar 02 '16 at 13:36
  • I thought I did provide a tiny example there. You know what an array is, how to append items to it, how to loop through an array, how to access an array index? You see the parallel between `divtxt1` and `divtxt[1]` (the latter being an array)? – deceze Mar 02 '16 at 13:52

0 Answers0