更改$ scope变量而不更新视图

时间:2014-06-17 04:52:00

标签: javascript angularjs

我有一项服务,根据某些条件发布一些事件。这段代码不起作用:

 $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毫秒的超时,然后它才能完美运行。我错过了什么或为什么会发生这种情况?

1 个答案:

答案 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()
                }

        });