Bobby Bruce
Bobby Bruce

Reputation: 361

Any idea why my polling doesn't work?

I have a this script:

var app = angular.module('MyApp', ['ui.bootstrap', 'ngCookies']);

app.service('FetchTeams', function($http, $interval) {
    this.url = 'data.json';

    this.fetch = function() {        
        return $http.get(this.url)
                .then(function(response) {
                    return response.data;
                })
                .catch(function(response) {
                    console.log('Error retrieving team data', response.status, response.data);
                });
    };

    this.startPolling = function(cb) {
                            this.fetch.then(cb);
                            $interval(function() { this.fetch().then(cb) }.bind(this), 60000); // start 1 min interval
                        };

    this.endLongPolling = function() { 
                            $interval.cancel(this.startPolling);
                          };    
});

app.controller('appcontroller', function($scope, FetchTeams) {
    FetchTeams.startPolling(function(data) {
        $scope.nflteams         = data.nflteams;
        $scope.nhlteams         = data.nhlteams;
        $scope.nbateams         = data.nbateams;
        $scope.mlbteams         = data.mlbteams;
    });
});

On lines 16 - 18, it should be polling data.json, but it currently isn't. Any idea what i'm doing what?

Thank you in advance!

Upvotes: 0

Views: 38

Answers (1)

fos.alex
fos.alex

Reputation: 5627

You are using this in the wrong contexts:

For example when you do:

         $http.get(this.url)

this does not have the url property because it points to the created function.

Try this:

    this.url = 'data.json';
    var that = this;

    this.fetch = function() {        
        return $http.get(that.url)
                .then(function(response) {
                    return response.data;
                })
                .catch(function(response) {
                    console.log('Error retrieving sales data', response.status, response.data);
                });
    };

You have that error two or three times in the whole script.

Upvotes: 1

Related Questions