在Angular中列出Rails API数据

时间:2014-10-08 13:30:19

标签: angularjs ruby-on-rails-4

我得到了这个例外:

Expected response to contain an array but got an object

怎么把 isArray:true 如果生病使它变得虚假和来源,这个例外就会消失。

{"themes":[{"id":1,"name":"This is first then","description":"This is first then"},{"id":2,"name":"Second Themes","description":"Second Themes"},{"id":3,"name":"This is $th theme","description":"This is $th theme"}]
}

但我不确切知道如何从这个json中检索数据。

<tr ng-show="themes.length" ng-repeat="theme in themes">
                <td>{{theme.name}}</td>
                <td>{{theme.description}}</td>
            </tr>

它什么也没显示。

请帮忙

2 个答案:

答案 0 :(得分:0)

$scope.obj = {"themes":[{"id":1,"name":"This is first then","description":"This is first then"},{"id":2,"name":"Second Themes","description":"Second Themes"},{"id":3,"name":"This is $th theme","description":"This is $th theme"}]}

<tr ng-show="obj.themes.length > 0" ng-repeat="theme in obj.themes">
     <td>{{theme.name}}</td>
     <td>{{theme.description}}</td>
</tr>

答案 1 :(得分:0)

尝试修改 json 响应,如下所示:

[{"id":1,"name":"This is first then","description":"This is first then","created_at":"2014-10-08T10:37:08.208Z"},{"id":2,"name":"Second Themes","description":"Second Themes","created_at":"2014-10-08T11:28:54.900Z"},{"id":4,"name":"4th theme","description":"4th theme","created_at":"2014-10-09T05:13:35.605Z"}]

你可以从rails themes / index.json

中做到这一点
object false
child(@themes => :themes) do
  extends "themes/show"
end

将其修改为:

object @themes
attributes :id, :name, :description, :created_at

并在资源

中制作isArray: true