2016-06-21 36 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

相關問題