I am not able to iterate over the values of a model I am updating. I have the following controller
app.controller('VariantsController', ['$http', function($http){
var ctrl = this;
this.cars = []; // I get the cars eventually ...
this.selected_car = null;
this.selectCar = function(car){
ctrl.selected_car = car;
}
When the user clicks on a "car" on the webpage, I update the controller's selected_car
. The page updates appropriately to show the name of the selected car, but ng-repeat does not iterate over the selected_car
's properties.
The following is the relevant html (variants
is the alias of the controller):
<div ng-show="variants.selected_car">
<h6>{{variants.selected_car.name}}</h6> <!-- Updates without problem -->
<table class="table">
<tr ng-repeat="(key,val) in variants.selected_car.specs"> <!-- Problem! Does not iterate -->
<td>{{key}}</td>
<td>{{val}}</td>
</tr>
</table>
</div>
I looked into this: AngularJS: ng-repeat list is not updated when a model element is spliced from the model array but the accepted answer states "Whenever you do some form of operation outside of angularjs ...". In this case, I am updating the model from within the controller so it should not have been a problem. Also, how would I use $apply
in this case when I'm not explicitly using $scope
?
selected_car.specs
is an object with a lot of keys:
{
...
mileage: 13.53,
mileage_city: 23,
mileage_highway: 32,
power: 220,
...
}
Plunker (courtesy @Phil): http://plnkr.co/edit/ULSr2Tk5D7phTYW0t7Ys?p=preview