On submitting a $Modal (modalInstance) I am updating an array on the scope ($scope.expenses) the view has its binding to the object, datatable uses "expenses" as data. I can see that the $scope.expenses is getting updated on debug and a new item is being inserted but in the table and in a {{expenses.length}} bind I cant see the update. I have read many questions about this issue, The insert is being executed on angular side so if I understand apply\digest is not needed.
I tried:
- calling $scope.$digest\$scope.$apply() after the push (Resulting an error that digest is already in progress)
- wrap the push in a function in $scope.$apply(function). Same error, digest already in progress
- Use $timeout so the push will get postponed for next digest cycle - view is not getting updated
None of the options above solved the issue.
View (I do see the length before the push so the bind is defined correctly in the view, thats why I don't paste the entire view):
<div class="row">
<div class="ibox-content col-lg-10 col-lg-offset-1" ng-controller="LiveCtrl">
{{expenses.length}}
<div class="col-md-1" ng-controller="LiveCtrl">
<button class="btn btn-link" ng-click="openAddExpenseModal()">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Expense
</button>
</div>
</div>
</div>
Controller code where I push to expenses the new item:
modalInstance.result.then(function (newExpense) {
ExpenseService.addExpense(newExpense).then(function (id) {
console.log(id);
$scope.expenses.push(newExpense);
}, function (rejectData) {
console.log(rejectData);
});
}, function (msg) {
console.log('Modal dismissed at: ' + new Date() + 'Message - ' + msg);
});
{{expenses.length}}?