3

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?

1
  • 1
    you use one message object Commented Jan 25, 2016 at 16:42

2 Answers 2

4

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>

Sign up to request clarification or add additional context in comments.

Comments

0

Well, you don't have vm.message defined anywhere? I think it might also be an idea to store your submit function as part of vm too:

(function(){
    'use strict';

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

    function AppController() {

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

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

    }
})(); 

1 Comment

note: you should update vm.message in submit function with new object otherwise you get same behavior as in question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.