In AngularJS, I am trying to add a new item to an array. The item gets added correctly, but the display does not reflect the change.
Add new task:
$scope.addNewTask = function()
{
console.log( "Before: " + $scope.tasks.length );
$scope.newTask = [
{
id: '3',
title: 'Get answer'
}
];
$scope.tasks.push( $scope.newTask[0] );
console.log( "After: " + $scope.tasks.length ); // one more than before
};
Task array:
$scope.tasks = [
{
id: '1',
title: 'Run into problem'
},
{
id: '2',
title: 'Ask question'
}
];
View:
<ul>
<li ng-repeat="task in tasks>
{{ task.id }} {{ task.title }}
</li>
</ul>
While I can see on the console that the new item was added, the list is not updating to reflect the change.
Edit: Don't know if it makes any difference, but the function is called upon clicking the submit button of a form as follows:
<form class="newtask-form" ng-submit="addNewTask()">
<input type="submit" name="submit" value="Add new task" />
</form>