Angular - REST Service XML Links with $Resource

谁说我不能喝 提交于 2019-12-11 11:23:25

问题


I am using Angular with SilverStripe for my backend. I am using this module https://github.com/silverstripe/silverstripe-restfulserver for my REST API instead of building it myself.

When the API returns a list with a many_many relationship it looks like

<Recipe href="http://localhost/groceryList/api/v1/Recipe/2.xml">
<Title>Chilli</Title>
<ID>2</ID>
<Ingredients linktype="many_many" href="http://localhost/groceryList/api/v1/Recipe/2/Ingredients.xml">
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/5.xml" id="5"/>
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/6.xml" id="6"/>
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/7.xml" id="7"/>
</Ingredients>
</Recipe>

My question is 2 part.

  1. Why is the collection of ingredients a bunch of links? I was hoping it would contain the name of each ingredient. Is this how REST is suppose to work?

  2. Using Angular $Resource, how can easily get the ingredients to loop through using ng-repeat?


回答1:


First you need to convert your XML to JSON with https://code.google.com/p/x2js/ or https://github.com/johngeorgewright/angular-xml, so you need an HttpInterceptor to convert all your requests and responses from the server.

I don't know why you don't have the name of each ingredient into your response, I think you need to change the response returned by your API, but I don't know how SilverStripe work.



来源:https://stackoverflow.com/questions/28799344/angular-rest-service-xml-links-with-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!