0

基本上,我有一个 SPA,它通过 JSON 从 Rails 站点获取其内容。现在我有一个工厂加载一个事件列表,其格式类似于:

{
id: 35987,
class: "Event",
event_id: 35987,
name: "New Event",
preview: "Description of the event goes here!"
},
{
id: 35989,
class: "Event 2",
event_id: 35989,
name: "Event for Something else",
preview: "Description of the event goes here again!"
}

然后,我在视图上显示前 5 个事件,控制器从资源中获取前 5 个事件。

  .controller('UpcomingCtrl', function($scope, upcomingFactory, localUserFactory) {

    localUserFactory.get(function (user) {
        upcomingFactory.query({channelID: user.channel_id}, function(data){
           $scope.upcomingEvents = data;
        });
    });

  })

这对我来说是棘手的部分。我希望用户能够点击这 5 个结果之一,然后被带到相应事件的详细信息页面。此详细事件的信息来自一个单独的 JSON 文件,该文件使用此结构http://www.testsite.com/api/v1/ {{event_id}}.json由事件 ID 标记

所以这意味着我需要以某种方式将事件列表中的事件 ID 传递给资源,并根据用户单击的 URL 为事件 .json 文件填写 {{event_id}}。这意味着路由也必须是动态的,这样当您单击链接时,http://www.testsite.com/events 上的任何事件都可以变为http://www.testsite.com/events/id 。

我希望这是有道理的!

4

1 回答 1

1

使用 url 映射器注册您的资源:

$resource('testsite.com/events/:id);

您可以通过向资源 get 调用提供对象来使用该 url 映射:

myResource.get({id: oneId}).then(... 

该对象中未在资源 url 中定义的任何其他属性都将作为请求 url 末尾的查询字符串参数。

编辑:

我想我误解了你的问题。您可以使用我上面描述的相同 url 映射来注册您的应用程序路由。当用户单击事件时,请执行以下操作:

$location.path(route + '/' + eventId);

在详细控制器上,注入 $routeParams 服务并访问您的事件 id 作为属性,例如:

$scope.eventId = $routeParams.id // or instead of id, the name of the url mapping you registered in your routes ('events/:nameOfVariable') 
于 2014-04-02T01:06:11.110 回答