我有一项服务,根据某些条件发布一些事件。这段代码不起作用:
$scope.$on('INTEREST_RECEIVED', function (event, data) {
if ($scope.user.userID == data.otherUserID) {
console.log($scope);
$scope.showAcceptDeclinePanel = true;
$scope.showInterestYesNoPanel = false;
}
});
但是这个确实:
$scope.$on('INTEREST_RECEIVED', function (event, data) {
$timeout(function () {
if ($scope.user.userID == data.otherUserID) {
console.log($scope);
$scope.showAcceptDeclinePanel = true;
$scope.showInterestYesNoPanel = false;
}
}, 50);
});
我必须保持50毫秒的超时,然后它才能完美运行。我错过了什么或为什么会发生这种情况?
答案 0 :(得分:2)
在触发事件后尝试在您的示波器上调用$ apply:
$scope.$on('INTEREST_RECEIVED', function (event, data) {
if ($scope.user.userID == data.otherUserID) {
console.log($scope);
$scope.showAcceptDeclinePanel = true;
$scope.showInterestYesNoPanel = false;
$scope.$apply()
}
});