Array push, duplicate elements

pmqa

This is my HTML:

<body ng-app="app">

<div ng-controller="AppController as vm">

    <form name="testForm" ng-submit="vm.submit(vm.message)" class="form-group">
        <textarea ng-model="vm.message.text" placeholder="text" class="form-control"></textarea>
        <input type="hidden" ng-model="vm.message.date">
        <input type="submit" class="btn btn-primary">
    </form>

    <table class="table table-stripped table-bordered">
        <tr ng-repeat="message in vm.messages track by $index">
            <td>{{ message.text }}</td>
            <td>{{ message.date }}</td>
        </tr>
    </table>

</div>

This is my controller:

    (function(){
    'use strict';

    angular.module('app', [
        'ngRoute'
    ]).controller('AppController', AppController);

    function AppController() {

        var vm = this;        
        vm.submit = submit;                
        vm.messages = [];

        function submit(elem) {
            elem.date = new Date();
            vm.messages.push(elem);
        }  

    }
})();  

I don't get it, whenever I enter and submit a message object, all the objects inside the vm.messages array become that object.

I want to save all the objects inside the array without any change. What am I doing wrong?

Grundy

You use just one message object, so you always add reference to it.

Just create new object, on submit

    (function(){
    'use strict';

    angular.module('app', [
        
    ]).controller('AppController', AppController);

    function AppController() {

        var vm = this;        
        vm.submit = submit;                
        vm.messages = [];
        vm.message = {}; //create object

        function submit(elem) {
            elem.date = new Date();
            vm.messages.push(elem);
            vm.message = {}; //create new object
        }  

    }
})();  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<div ng-app="app" ng-controller="AppController as vm">

    <form name="testForm" ng-submit="vm.submit(vm.message)" class="form-group">
        <textarea ng-model="vm.message.text" placeholder="text" class="form-control"></textarea>
        <input type="hidden" ng-model="vm.message.date">
        <input type="submit" class="btn btn-primary">
    </form>

    <table class="table table-stripped table-bordered">
        <tr ng-repeat="message in vm.messages track by $index">
            <td>{{ message.text }}</td>
            <td>{{ message.date }}</td>
        </tr>
    </table>

</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Duplicate array elements in Ruby

From Dev

Duplicate Elements in Array (SML)

From Dev

Store duplicate array elements

From Dev

removing duplicate array elements

From Dev

JS - Duplicate Elements in the Array

From Dev

Delete duplicate elements in an array

From Dev

Remove duplicate elements in array

From Dev

Store duplicate array elements

From Dev

JS - Duplicate Elements in the Array

From Java

push multiple elements to array

From Dev

array elements push in android

From Dev

How to duplicate elements in a js array?

From Dev

Partition an Array with duplicate elements into arrays with unique elements

From Dev

Duplicate elements of an array except the first and last elements

From Dev

push only unique elements in an array

From Dev

Javascript push to elements in a multidimensional array

From Dev

Push values of clicked elements in array

From Dev

Angularjs push list elements to an array

From Dev

Array push alters all elements

From Dev

angular is not able to push duplicate items in array

From Dev

Add elements to a dynamic array with array_push

From Java

Removing duplicate elements from an array in Swift

From Java

How to find duplicate elements length in array flutter?

From Dev

How to count array duplicate elements in MongoDB

From Dev

How to count duplicate array elements? (javascript)

From Dev

Find Duplicate Elements In Array Using Swift

From Dev

How is this code getting rid of duplicate array elements?

From Dev

Php count duplicate elements in an array and concat of ids

From Dev

Array field in rails eliminating duplicate elements on edit