数据绑定不起作用?

时间:2014-05-30 12:42:04

标签: angularjs

浏览器仅向页面呈现{"data":[]},并且在解析承诺后不会更新,但window.sc显示数据已获取并成功添加到作用域,但浏览器未显示这个。为什么会这样?

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')
        div(ng-controller='PlayersCtrl')
            h1 {{players}}
    script(src='index.js')

index.js.coffee

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

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

fantasyCricket.factory('Players', () ->
    obj = {
        data: []
        refresh: () ->
            (new Parse.Query(Parse.Object.extend('player'))).find()
    }
    obj
)

fantasyCricket.controller('PlayersCtrl', ['$scope', 'Players', ($scope, Players) ->
    $scope.players = Players
    window.sc = $scope

    Players.refresh().done((players) ->
        $scope.players.data.splice.apply($scope.players.data, [0, 0].concat(players))
    ).fail((err) -> alert('Failed to load players'))
])

1 个答案:

答案 0 :(得分:1)

尝试在刷新回调中调用$ scope。$ apply

Players.refresh().done((players) ->
        $scope.players.data.splice.apply($scope.players.data, [0, 0].concat(players))
        $scope.$apply();
    ).fail((err) -> alert('Failed to load players'))

你也可以使用push而不是splice

 $scope.players.data.push.apply($scope.players.data,players);
 $scope.$apply();