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

  • Adding a function to Array.prototype in IE results in it being pushed in to every array as an element 2016-02-01

    I have added the following polyfill to Array in the beginning of my project: if (!Array.prototype.find) { Array.prototype.find = function(predicate) { if (this === null) { throw new TypeError('Array.prototype.find called on null or undefined'); } if

  • Perl6: Pushing an array to an array of arrays with one element seems not to work as expected 2016-01-19

    Why do I get this kind of data-structure, when I push arrays to an array of arrays which has one array as its only element? use v6; my @d = ( [ 1 .. 3 ] ); @d.push( [ 4 .. 6 ] ); @d.push( [ 7 .. 9 ] ); for @d -> $r { say "$r[]"; } # 1 # 2 # 3

  • array with reusable elements 2013-10-20

    I need array to store elements, but I don't want to make memory allocations at runtime. So I reuse elements. Some fields are never changes so as I reuse elements I need to assign those fields just once. This is what I wrote, only sceleton, need to ad

  • List Manipulation on Array with subscript elements 2014-10-05

    I am trying to write a function which operates on an array with subscripted elements. As an example i have the array a={$1_3$,$2_2$,$4_1$}, which means i have 3 amounts of value 1, 2 amounts of value 2 and 1 amount of value 4. If the subscripted amou

  • Group nested arrays by first element 2016-01-21

    I have an array that contains individual 2-elements arrays, each contains a date in milliseconds in the first cell and a number (transaction number) in the 2nd cell. It looks like this: var array = [[12135435123, -2], [12135435123, 1], [12135464565,

  • Ruby/Rails: How to determine if one array contains all elements of another array? 2011-09-12

    Given: a1 = [5, 1, 6, 14, 2, 8] I would like to determine if it contains all elements of: a2 = [2, 6, 15] In this case the result is false. Is there any built-in Ruby / Rails method to identify such array inclusion ? One way to implement this is: a2.

  • Given two sorted arrays, add the elements of the second array into the first 2012-11-21

    In Cracking the Coding Interview by Gayle Laakmann McDowell, there's a question that asks you to write code for the following: Given two sorted arrays, A and B. Write a method merging the elements of B into A in sorted order. Assume A has a large eno

  • iterate over an array and delete elements conditionally 2013-01-04

    The following logic: Iterate over the URLs array and If the HTTP request with the given URL does not timeout, continue. If the URL times out, remove it from the array and continue with the next one until the array is exhausted. Has been implemented i

  • Big O notation allocate array of N element 2014-11-12

    In Big O notation, allocate an array of N element is defined by O(1) or O(n) ? For example in C#, if I allocate an array like this : int[] a = new int[10] When I display this array, I have : {0,0,0,0,0,0,0,0,0,0} --------------Solutions-------------

  • Adding more elements than allocated space 2016-01-17

    This question already has an answer here: What is the purpose of allocating a specific amount of memory for arrays in C++? 5 answers In my data structure class, teacher gave us a simple code to print elements from an array with a loop. I declared an

  • Adding Tenant in WSO2 programatically 2016-01-19

    I am trying to add a tenant in WSO2 programatically using java (i.e without using the API Manager console). I tried using "RemoteTenantManagerService" in the Admin Services but it gives an error: org.apache.axis2.AxisFault: org.wso2.carbon.user.

  • Cast value to array of unknown `Element` type 2016-01-19

    Assuming a function that operates on any Array: func g<T>(array: [T]) -> Void { // ... } ...and a function that receives a value of type Any: func f(x: Any) -> Void { if x is Array { // g(?) } } How can I get a properly typed version of x that

  • How to filter an array from all elements of another array 2016-01-20

    I'd like to understand the best way to filter an array from all elements of another one. I tried with the filter function, but it doesn't come to me how to give it the values i want to remove. Something Like: var array = [1,2,3,4]; var anotherOne = [

  • Algorithm which efficiently cycles through an array for matching elements based on constraints 2016-01-29

    I am doing a project and I've just now gotten around to optimizing. I was wondering if the community may be able to help. I have a single array and my goal is to efficiently iterate through the array, taking out elements if they happen to match. (Mat

  • How to push one array object value into another inside a loop using PHP 2016-02-05

    I need one help.I need to push one array object value into another array object in each iteration using PHP.I am explaining my code below. for($i=0;$i<$len;$i++){ while($report=mysqli_fetch_assoc($reportqry)){ $result[]=$report; } //$arry } Here i ne

  • I have been asked to make an Create a new integer array with 16 elements 2016-02-06

    Java code (not Java script). I was asked to create a new integer array with 16 elements. Only integers between 1 and 7 are to be entered in the array from user (scanner)input. Only valid user input should be permitted, and any integers entered outsid

  • javascript objects are pushed into array as referrence 2016-02-09

    EDIT:: I forgot to add, this is for the NODE JS Server side, some answers has the Jquery cloning (does jquery cloning works on the server side, i tried it is throwing error as ReferenceError: jQuery is not defined). So , i request every one, please a

  • Assigning elements of one array to an elements of a 2d array 2016-02-14

    For this piece of my project, I have to assign elements of one dynamically allocated array to another part of a dynamically allocated 2D array. I've researched and read but cannot find anything relating to my question. When I attempt to run the progr

  • Adding push to array inside the class that is being pushed 2011-09-12

    I used to mostly do this: class Person constructor : (@parent, @data)-> @index = @parent.length class People constructor : -> people = [] for data in database.people people.push new Person(people, data) Lately I have been trying the following: class

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.759 (s). 13 q(s)