Why do I get in to a digetst loop when I repeat over a cloned array?
angular.module('myApp', [])
.controller('Ctrl1', function(MyService) {
var $ctrl = this;
$ctrl.getData = MyService.getData;
})
.service('MyService', function () {
var data = [
{
name: 'Adam',
age: 12
}, {
name: 'Bob',
age: 14
}, {
name: 'Caesar',
age: 15
}];
this.getData = function () {
// This is the offending line. If I remove the call to
// angular.copy it works but in my real scenario I want to
// modify the copy of `data` before returning it, without
// affecting the original version. That's why I need to clone
// it first.
return angular.copy(data);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
<div ng-app="myApp" ng-controller="Ctrl1 as $ctrl">
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr ng-repeat="data in $ctrl.getData()">
<td>{{data.name}}</td>
<td>{{data.age}}</td>
</tr>
</table>
</div>