I have two arrays in my controller and a third array which is formed by concatenating the first two arrays.
var app = angular.module('app', []);
var MainController = app.controller('MainController', function($scope) {
$scope.dogs = ['Beagle', 'Poodle', 'Boxer'];
$scope.cats = ['Maine Coon', 'Ragdoll', 'Bengal'];
$scope.pets = $scope.dogs.concat($scope.cats);
$scope.createDog = function() {
$scope.dogs.push('Rottweiler');
};
});
I then output all this data using ng-repeat
.
<h3>Dogs ({{dogs.length}})</h3>
<ul>
<li ng-repeat="dog in dogs">{{dog}}</li>
</ul>
<button ng-click="createDog()">Add a dog</button>
<hr>
<h3>Cats ({{cats.length}})</h3>
<ul>
<li ng-repeat="cat in cats">{{cat}}</li>
</ul>
<hr>
<h3>Pets ({{pets.length}})</h3>
<ul>
<li ng-repeat="pet in pets">{{pet}}</li>
</ul>
When I click the button and add a dog to the $scope.dogs
array, the list of pets never gets updated.
How can I concat two collections of objects while preserving angular's bindings so that the pets will update?