为什么我会收到infdig错误?

时间:2014-05-30 15:07:29

标签: angularjs

我收到infdig错误,我在下面的代码中有scope.$apply。为什么会这样?我已经尝试了很多东西并浏览了很多文档和博客,但我似乎仍然无法弄明白:(

index.js.coffee

fantasyCricket = angular.module('fantasyCricket', [() ->
    # Initialises Parse SDK.
    Parse.initialize("####", "####")

    # Initialises Parse FacebookUtils.
    Parse.FacebookUtils.init({
        appId: "####"
        cookie: true
        xfbml: true
    })
])

fantasyCricket.filter('sortByCricket', () ->
    lastOutput = null
    lastInput = null
    (cricketers) ->
        if lastInput isnt cricketers
            lastOutput = cricketers.sort((a, b) ->
                pts = b.total - a.total
                if pts is 0 then a.value - b.value else pts
            )
        else
            lastOutput
)

fantasyCricket.factory('Players', () ->
    found = false
    obj = {
        data: {
            all: []
        }
        getAll: (scope, force) ->
            if (found is false) or (force is true)
                (new Parse.Query(Parse.Object.extend('player'))).find().done((players) ->
                    obj.data.all.push.apply(obj.data.all, players.map((player) ->
                        newPlayer = player.attributes
                        newPlayer.id = player.id
                        newPlayer.total = newPlayer.batPts + newPlayer.bwlPts + newPlayer.fldPts + newPlayer.wins + (newPlayer.matches * 2)
                        newPlayer
                    ))
                    found = true
                    scope.$apply()
                ).fail((err) -> alert('Failed to load players.'))
    }
)

fantasyCricket.factory('Settings', () ->
    found = false
    obj = {
        data: {
            players: 0
            budget: 0
            transfers: 0
        }
        get: (scope, force) ->
            if (found is false) or (force is true)
                (new Parse.Query(Parse.Object.extend('setting'))).find().done((settings) ->
                    settings.forEach((setting) ->
                        obj.data[setting.get('key')] = setting.get('value')
                    )
                    found = true
                    scope.$apply()
                ).fail((err) -> alert('Failed to load settings.'))
    }
)

fantasyCricket.controller('PlayersCtrl', ['$scope', 'Players', ($scope, Players) ->
    $scope.players = Players.data
    Players.getAll($scope)
])

fantasyCricket.controller('InfoCtrl', ['$scope', 'Settings', ($scope, Settings) ->
    $scope.settings = Settings.data
    Settings.get($scope)
])

index.html.jade

head
    meta(charset='utf-8')
    title Badsey Fantasy Cricket
    script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js')
    script(src='https://www.parse.com/downloads/javascript/parse-1.2.18.min.js')
    script(src='vendor/facebook/all.js')
    link(rel='stylesheet', href='index.css')

body()
    div(ng-app='fantasyCricket')
        #info(ng-controller='InfoCtrl')
            | Budget: {{settings.budget}},
            | Transfers: {{settings.transfers}},
            | Players: {{settings.players}}

        #players(ng-controller='PlayersCtrl')
            table
                thead
                    tr
                        th.name Name
                        th.more MP
                        th.more Wins
                        th.more Bat
                        th.more Bwl
                        th.more Fld
                        th Total
                        th Value
                tbody
                    tr(ng-repeat='player in (players.all | sortByCricket)' class='{{true ? "Yolo" : "Troll"}}')
                        td.name {{player.name}}
                        td.more {{player.matches}}
                        td.more {{player.wins}}
                        td.more {{player.batPts}}
                        td.more {{player.bwlPts}}
                        td.more {{player.fldPts}}
                        td {{player.total}}
                        td {{player.value}}

    script(src='http://code.jquery.com/jquery-2.1.1.min.js')
    script(src='index.js')

1 个答案:

答案 0 :(得分:0)

问题是过滤器每次都提供不同的数组,因此导致循环。