[英]Strange Angular.js behavior with Faye
I have the following code. 我有以下代码。 The code is inside the controller. 该代码位于控制器内部。
var client = new Faye.Client('http://localhost:9292/faye');
client.subscribe('/main', function(message) {
console.log(message);
$scope.messages.push(message);
console.log($scope.messages);
});
Here I am using Faye to listen to channel and then to add an object to $scope
. 在这里,我使用Faye收听频道,然后将对象添加到$scope
。 But when I console.log
scope, I see that the message
object has been added to scope, but the DOM
is not refreshed, and Two-Way binding doesn't work. 但是,当我使用console.log
作用域时,我看到message
对象已添加到作用域中,但是DOM
没有刷新,双向绑定也不起作用。
How can I fix this? 我怎样才能解决这个问题?
Angular needs to trigger a digest cycle when an event occurs outside of the Angular world. 当事件发生在Angular世界之外时,Angular需要触发摘要循环。 Use $apply to trigger the digest: 使用$ apply触发摘要:
var client = new Faye.Client('http://localhost:9292/faye');
client.subscribe('/main', function(message) {
$scope.$apply(function() {
console.log(message);
$scope.messages.push(message);
console.log($scope.messages);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.