知道为什么我的民意调查不起作用吗?

时间:2015-10-21 19:10:19

标签: angularjs

我有一个这样的剧本:

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

在第16-18行,它应该是轮询data.json,但目前不是。知道我在做什么吗?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

你在错误的环境中使用它:

例如,当你这样做时:

&

这没有url属性,因为它指向创建的函数。

试试这个:

var data = {
    'name': 'root',
        'children': [{
        'name': 'level 1.a',
            'children': [{
            'name': 'level 2.a',
                'children': [{
                'name': 'level 3.a',
                'size': 3938
            }, {
                'name': 'level 3.b',
                'size': 3812
            }, {
                'name': 'level 3.c',
                'size': 743
            }]
        }, {
            'name': 'level 2.b',
                'children': [{
                'name': 'level 3.a',
                'size': 3534
            }, {
                'name': 'level 3.b',
                'size': 5731
            }]
        }]
    }, {
        name: 'level 1.b',
        children: []
    }]
};

var height = 300;
var width = 500;

var cluster = d3.layout.cluster()
    .size([height, width - 100]);

var diagonal = d3.svg.diagonal()
    .projection(function (d) {
    return [d.y, d.x];
});

var svg = d3.select('body').append('svg')
    .attr('height', height)
    .attr('width', width)
    .append('g')
    .attr('transform', 'translate(40, 20)');

var nodes = cluster.nodes(data);
var links = cluster.links(nodes);

var link = svg.selectAll('.link')
    .data(links)
    .enter().append('path')
    .attr('class', 'link')
    .attr('d', diagonal);

var node = svg.selectAll('.node')
    .data(nodes)
    .enter().append('g')
    .attr('class', 'node')
    .attr('transform', function (d) {
    return 'translate(' + d.y + ',' + d.x + ')';
});

node.append('circle')
    .attr('r', 4.5);

node.append('text')
    .attr('dx', 8)
    .attr('dy', 3)
    .text(function (d) {
    return d.name;
});

在整个脚本中,您有两到三次该错误。