繁体   English   中英

为什么angularJs $ watch只运行一次

[英]why angularJs $watch only run once

手表中的代码仅运行一次。 我该如何解决?

 this.$rootScope.$watch('tabType', () => {
            if (this.$rootScope["tabType"] === TabType.Sent) {
                this.$scope.refreshSentList();
            } else if (this.$rootScope["tabType"] === TabType.Archive) {
                this.$scope.refreshArchiveList();
            } else if (this.$rootScope["tabType"] === TabType.Inbox) {
                this.$scope.refreshInboxList();
            } else if (this.$rootScope["tabType"] === TabType.Snooz) {
                this.$scope.refreshSnoozList();
            } else if (this.$rootScope["tabType"] === TabType.Trash) {
                this.$scope.refreshTrashList();
            }
        },true);

因此,此问题的可能原因是$watch在控制器启动时无法在视图中找到该元素。 为了使$watch绑定/工作,该元素应已存在于视图中。

我的猜测-您必须在视图中使用ng-if ng-if仅在条件为true时创建元素。

我两次遇到此问题,两次都删除了ng-if

这是在ES5上运行的示例,如果我没记错的话,我会看到您使用ES6。

但是,这不应该影响$watch工作方式

$scope.$watch('tabType', function (newValue, oldValue) {
    console.log(newValue);
    console.log(oldValue);
});

另外随着this对开始时不与所必要的$watch

希望这对您有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM