angularjs push to array adding ten elements

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

Replay

Category: arrays Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.105 (s). 12 q(s)