我在appController中有这个代码。当正在进行HTTP时,代码将$ scope.cursorWait的值设置为true:
$scope.$on('cfpLoadingBar:started', function (event, data) {
$scope.cursorWait = true;
});
$scope.$on('cfpLoadingBar:completed', function (event, data) {
$scope.cursorWait = false;
});
我的连接服务也有这个。互联网断开连接时会调用这些函数:
isConnectedHandler = (): void => {
var self = this;
self.$rootScope.connected = true;
self.$rootScope.disconnected = false;
self.connectMessage = null;
self.minutes = 0;
}
isNotConnectedHandler = (): void => {
var retry = 0;
var self = this;
self.$rootScope.connected = false;
self.$rootScope.disconnected = true;
如果$ rootScope.disconnected或cursorWait为真,我怎么能监视$ rootScope.disconnected和cursorWait的值然后设置rootScope变量的值等于true?
答案 0 :(得分:1)
假设你的控制器有多个ViewModel,它想监控,比如说cursorWait
和connected
。在这种情况下,Angular为您提供watchGroup
能力。通过这种方式,您可以监控多个变量,如果其中一个变量发生变化,您可以根据需要做出反应。
示例代码 (使用Typescript进行演示)
$scope.$watchGroup([()=> { return this.cursorWait }, ()=> { return this.connected}],
(oldValues, newValues)=> {
/* The callback gets an array of `oldValues` and an array of `newValues`,
the index according to the variables you were watching */
});
有关更多信息,请参阅Angular documentation。