1

我只是在学习 AngularJS。我有以下 JSON 端点

/api/v1/leaderboards/:id

它返回与 ID 对应的排行榜。

我有以下 AngularJS 服务:

App.factory 'Leaderboard', ['$resource', ($resource) ->
  $resource '/api/v1/leaderboards/:id', id: '@id'
]

而这个 AngularJS 控制器:

App.controller 'LeaderboardsCtrl', ['$scope', 'Leaderboard', ($scope, Leaderboard) ->
  $scope.selectedLeaderboard = Leaderboard.query()
]

这是我的观点(在haml中):

.leaderboard{"ng-app" => 'Leaderboards', id:"ng-app"}
  %h4.title
    Leaderboard Title
  .leader-section{'ng-controller' => "LeaderboardsCtrl"}
    .leader-content{'ng-show' => "selectedLeaderboard", 'ng-repeat' => "leader in selectedLeaderboard"}
      .media.award-group
        %a.pull-left{href: "{{leader.user.path}}"}
          %img.media-object{src: "{{leader.user.small_pic_url}}"}
        .media-body
          %h4.media-heading
            %a.leader-name{href: "{{leader.user.path}}"}
              {{leader.user.name}}
            .metric.pull-right
              {{leader.count}}

当我加载页面时,我在控制台中看到了这个错误:

GET http://localhost:3000/api/v1/leaderboards 404 (Not Found)

如果我:id用实际的排行榜 ID 替换,那么一切正常。但是如何从视图级别传递该 ID?

4

1 回答 1

0
App.controller 'LeaderboardsCtrl', ['$scope', 'Leaderboard', ($scope, Leaderboard) ->
  $scope.selectedLeaderboard = Leaderboard.query({id: 'count-receiver'})
]

只需如上所述将 ID 传递给服务。

于 2013-08-06T19:44:23.630 回答