Reputation: 435
I have a piece of $scope text that should update dynamically.
<span ng-bind="user.balance"></span>
after pushing a button, new value of balance is received and should be displayed here.
$http.post('/transaction/send', transaction).success(function(sender) {
console.log("http response");
$scope.user = sender;
console.log($scope.user.balance);
$timeout(function() {$scope.$apply(); console.log("apply used");}, 1000);
});
In console log anything is received and displayed properly, but it still doesn't update scope in the view. What am I doing wrong? I'm using $timeout to get rid of "$digest is already in use"
Upvotes: 1
Views: 61
Reputation: 2008
You should use angular.extend(dst, src)
instead of assigning a new object to your $scope.user
variable. By doing so, you will be merging the new properties into your older object; this will keep your binding working. In the end, you should have something like this:
$http.post('/transaction/send', transaction).success(function(sender) {
console.log("http response");
angular.extend($scope.user, sender);
console.log($scope.user.balance);
});
Mind you, this only does a shallow merge, for a deep merge, you should look at this discussion or this one.
Upvotes: 2