2016-06-21 49 views
0

我看了一些关于如何正确执行此操作的示例,但绝对不会在我的结尾更新。我放置了一个断点,以确保它正在更新并通过工厂中的定时器并正确更新。我不应该用$ watch来对吗?如果有人能帮我弄清楚发生了什么,现在就可以帮助我解决头痛问题。Angular:通过工厂变量更新控制器作用域变量

app.factory('FoundationSystemStatusFactory', ['$timeout', '$q', 'SystemStatusFactory', function ($timeout, $q, SystemStatusFactory) { 
var service = {}; 

service.Count = 0; 

service.Ping = 0; 

service.PollingTest = function() { 
    $timeout(function() { 

     SystemStatusFactory.PingIP('www.google.com') 
      .then(function (data) { 
       service.Ping = data.data; 
       service.Count++; 
      }, function (data) { 
       service.Ping = data.data; 
      }); 

     service.PollingTest(); 
    }, 2000); 
} 

return service; 

}]); 

控制器

FoundationSystemStatusFactory.PollingTest(); 

$scope.ping = FoundationSystemStatusFactory.Ping; //NOT UPDATING 

$scope.count = FoundationSystemStatusFactory.Count; //NOT UPDATING 

编辑:尝试作为服务,仍然无法得到它的工作:

var self = this; 

self.Count = 0; 

self.Ping = 0; 

self.PollingTest = function() { 
    $timeout(function() { 

     SystemStatusFactory.PingIP('www.google.com') 
      .then(function (data) { 
       self.Ping = data.data; 
       self.Count++; 
      }, function (data) { 
       self.Ping = data.data; 
      }); 

     self.PollingTest(); 
    }, 2000); 
} 
+0

您是否尝试过使用服务,而不是工厂? –

+0

@MedetTleukabiluly现在不让我试试。我一直在查找两者之间的差异..而不是它被实例化的方式,我仍然对两者之间的差异感到困惑。 – user1189352

+0

简单工厂返回数据,服务修改数据 –

回答

1

您可以使用观察家:

$scope.$watch('FoundationSystemStatusFactory.Ping', function(newValue) { 
    $scope.ping = newValue; 
}); 

或者您可以使用参考工厂:

$scope.status = FoundationSystemStatusFactory; 

$interval(function() { 
    console.log($scope.status.Ping); // gets updated 
});  
+0

我知道$手表的作品,但我相信有一个正确的方式来完成我想要的而不必使用$ watch?谢谢你的回答 – user1189352

+0

@ user1189352你见过我的第二种方法吗?对你起作用吗? –

+0

嗨对不起,我做了。我相信这样做也行得通,如果我不能使用它,我会使用你的方法。我只是不想“破解”它,而是试图学习/以适当的方式做到这一点..我很积极,我应该能够使它工作,而不必使用$间隔。 – user1189352

2

一种不同的方法 - 事件

app.factory('FoundationSystemStatusFactory', ['$rootScope', '$timeout', '$q', 'SystemStatusFactory', function ($rootScope, $timeout, $q, SystemStatusFactory) { 
var service = { 
Count: 0 
}; 

service.PollingTest = function() { 
    $timeout(function() { 
     SystemStatusFactory.PingIP('www.google.com') 
      .then(function (data) { 
       $rootScope.$broadcast('FoundationSystemStatus:ping', data.data); 
       service.Count++; 
      }).catch(function (data) { 
       $rootScope.$broadcast('FoundationSystemStatus:ping', data.data); 
      }); 

     service.PollingTest(); 
    }, 2000); 
} 

return service; 

}]); 

//On controller... 
$scope.$on('FoundationSystemStatus:ping', function(ping){ 
$scope.ping = ping; 
}); 
+0

谢谢你的分享! – user1189352

0

好的,我发现如何做一些更多的研究后。对象被引用为数字,而字符串则不被引用。

app.factory('FoundationSystemStatusFactory', ['$timeout', '$q', 'SystemStatusFactory', function ($timeout, $q, SystemStatusFactory) { 
var service = {}; 

service.Data = { 
    Count: 0, 
    Ping: 0 
} 

service.PollingTest = function() { 
    $timeout(function() { 

     SystemStatusFactory.PingIP('www.google.com') 
      .then(function (data) { 
       service.Data.Ping = data.data; 
       service.Data.Count++; 
      }, function (data) { 
       service.Data.Ping = data.data; 
      }); 

     service.PollingTest(); 
    }, 2000); 
} 

return service; 
}]); 

控制器

app.controller('SystemStatusController', ['$scope', '$rootScope', '$timeout', 'FoundationSystemStatusFactory', 
    function ($scope, $rootScope, $timeout, FoundationSystemStatusFactory) { 

     FoundationSystemStatusFactory.PollingTest(); 

     $scope.data = FoundationSystemStatusFactory.Data; 
}]); 

查看

{{data.Ping}} 
{{data.Count}} 
+0

这基本上是我建议你的。另外,对不起,但短语“对象被称为数字和字符串不是”。没有意义。 –

+0

@goliney好的,我给你答案 – user1189352