I'm new to AngularJS, and this has been driving me nuts for several days. HELP!!! I've condensed the problem into as simple a code snippet as I could.. I've set up an array called indexArray in the Controller, and I want to push values to it from HTML via the function pushIndex(). The problem is that the push function adds ten identical elements to the array. When I list the array with the {{indexArray}} statement after the push, it looks like this:
[4,5,6,7,9,9,9,9,9,9,9,9,9,9]
Where I only wanted one "9" pushed, it added ten of them.
The same thing happens with strings, quotes, etc.
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
var app = angular.module('myDataApp',[]);
app.controller('myDataController',function($scope, $http){
$scope.indexArray=[4,5,6,7];
$scope.pushIndex=function(num){
$scope.indexArray.push(num);
};
});
</script>
</head>
<body>
<div ng-app="myDataApp" ng-controller="myDataController">
{{ pushIndex(9) }}
{{ indexArray }}
</div>
</body>
</html>
Sorry in advance if the solution is painfully simple or this question has been asked before. Thanks! -UDM
{{ pushIndex(9) }}expression many, many times (it won't even stop at 10). See this question for example.