作用域不在$ apply上更新使用

我有一块$ scope文本应该dynamic更新。

<span ng-bind="user.balance"></span> 

按下button后,新的平衡值被接收,并显示在这里。

 $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); }); 

在控制台日志中,任何东西都会被正确接收和显示,但是它仍然不会在视图中更新范围。 我究竟做错了什么? 我正在使用$超时摆脱“$摘要已被使用”

您应该使用angular.extend(dst, src)而不是将新的对象分配给$scope.uservariables。 通过这样做,你将把新的属性合并到你的旧对象中; 这将保持你的绑定工作。 最后,你应该有这样的事情:

 $http.post('/transaction/send', transaction).success(function(sender) { console.log("http response"); angular.extend($scope.user, sender); console.log($scope.user.balance); }); 

请注意,这只是一个浅层的合并,对于一个深度合并,你应该看看这个讨论或者这个 。