0

部分代码:

从服务部分:

$resource('/Serializer/community/V1/latestPost/:start/:end,{
},{
    query: {method:'GET', 
            params:{start:new Date().getTime(), end:1}, 
            isArray:true}
})

从控制器部分:

Post['latestPost_timestamp'].query({start: minTime,end:1},function (data) { 
    alert(JSON.stringify(data));
});

结果:

从控制台或浏览器上打开的链接:[3993,3983,3974,3964,3954,3944,3934,3926,3920,3910]

从控制器:[{},{},{},{},{},{},{},{},{},{}]

为什么它在控制器中返回空 {}?我希望它返回 [3993,3983,3974,3964,3954,3944,3934,3926,3920,3910] 但不是 [{},{},{},{},{},{}, {}、{}、{}、{}]。

任何人都可以帮忙吗?谢谢

4

1 回答 1

1

那是因为 $resource 应该是一个对象而不是原语,就像您的情况中的数字一样。如果资源可能是原始资源,那么您将无法同时执行此操作:

var posts = Post.query({start: minTime,end:1}, function() {
    for (var i = 0; i < posts.length; i++) {
        var post = posts[i];
        // If post here should be equal to for example 3993 in
        // your JSON, you wouldn't be able to call any methods
        // on it. The following two statements sort if illustrates it:
        assert(post === 3993);
        post.$save();
    }
});

因此,资源是一个对象,因此您可能应该更改服务器代码以返回类似的[{id: 3993}, {id: 3983}, ...]内容,以便您可以通过post.id.

于 2013-02-26T07:28:09.970 回答