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?