gpt4 book ai didi

javascript - 更改 $scope 变量不更新 View

转载 作者:行者123 更新时间:2023-12-02 17:17:46 25 4
gpt4 key购买 nike

我有一项服务,可以根据特定条件发布某些事件。此代码不起作用:

 $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 毫秒的超时,然后它才能正常工作。我是否遗漏了什么或者为什么会发生这种情况?

最佳答案

尝试在事件触发后在您的作用域上调用 $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()
}

});

关于javascript - 更改 $scope 变量不更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com