watch 中的代码只运行一次。 我该如何解决?
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
在 Controller 启动时无法在 View 中找到该元素。要使 $watch
绑定(bind)/工作,元素应该已经存在于 View 中。
我的猜测 - 您必须在 View 中使用 ng-if
。而 ng-if
只有在条件为真时才创建元素。
我两次遇到这个问题,两次都删除了 ng-if
。
关于javascript - 为什么 angularJs $watch 只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39061019/