0

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);
}
6
  • Have you tried to use service instead of factory? Commented Jun 21, 2016 at 18:22
  • @MedetTleukabiluly no let me try right now. I keep looking up the difference between the two.. and other than the way that it's instantiated i still am confused at the difference between the two. Commented Jun 21, 2016 at 18:24
  • Simply factory returns data, service modifies data Commented Jun 21, 2016 at 18:25
  • It's not just about changing app.factory to app.service, you have change the logic, read some docs, it's very easy Commented Jun 21, 2016 at 18:27
  • @MedetTleukabiluly okay looking into it, thanks for the tip Commented Jun 21, 2016 at 18:29

3 Answers 3

2

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;
});
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

i know $watch works, but i believe there's a proper way of accomplishing what i want without having to use $watch? thank you for your answer
@user1189352 have you seen my second approach? Does it work for you?
hi sorry yes i did. I'm sure that works as well and i'll use your approach if i can't get it to work. I just dont want to "hack" it and rather try to learn / do it the proper way.. I'm positive I should be able to make it work without having to use $interval neither.
I included $interval only for demonstration purposes. The thing is, that when you write $scope.status = FoundationSystemStatusFactory;. $sope.status contains reference to factory. And when property of the factory changes, reference contains fresh non-copied data. Because basically reference is a link to a memory cell. When you write $scope.ping = FoundationSystemStatusFactory.Ping;. $scope.ping contains a value (not a reference). It is happening because type of FoundationSystemStatusFactory.Ping === 'number'.
thank you for the explanation! helps put things in perspective
0

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

2 Comments

It is basically what I've suggested you. Also, sorry but phrase "Objects are referenced as numbers and strings are not." makes no sense.
@goliney ok i gave you the answer then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.