user1189352
user1189352

Reputation: 3875

Angular: Updating controller scope variable through a factory variable

I looked into examples on how to do this properly but it's definitely not updating on my end. I put a breakpoint to make sure it's updating and going through the timer in the Factory and it's updating properly. I shouldn't have to use $watch right? If someone can help me figure out what's going on it would help with my headache right now lol thanks.

Factory

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;

}]);

Controller

 FoundationSystemStatusFactory.PollingTest();

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

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

EDIT: tried as Service, still couldn't get it to work:

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);
}

Upvotes: 0

Views: 72

Answers (3)

user1189352
user1189352

Reputation: 3875

Okay I found out how to do it after some more research. Objects are referenced as numbers and strings are not.

Factory

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;
}]);

Controller

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

        FoundationSystemStatusFactory.PollingTest();

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

View

{{data.Ping}}
{{data.Count}}

Upvotes: 0

Muli Yulzary
Muli Yulzary

Reputation: 2569

A different approach - events.

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;
});

Upvotes: 2

Serhii Holinei
Serhii Holinei

Reputation: 5864

You can use watcher:

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

Or you can use reference to factory:

$scope.status = FoundationSystemStatusFactory;

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

Upvotes: 1

Related Questions