问题描述
我采用了棱角分明消耗在同一个应用程序一个RESTful API。我对联系人$资源设置
在 http://sample-site.com/api/contacts
资源
I'm using Angular to consume a RESTful API on the same application. I have a $resource setup for the contacts
resource at http://sample-site.com/api/contacts
这是伟大的,它的工作原理,但是我需要用 / API /联系人
应用程序的不同的页面。
This is great and it works, however I need to interact with the basic CRUD of /api/contacts
on different pages of the application.
例如,我需要再次接触在另一页上 HTTP托管网络应用程序进行交互://样品现场/朋友/ {朋友-ID}
。然而,当我尝试使用我的联系方式的资源,页面上的URL资源追加到的电流网址:
For example, I need to interact with contacts again at another page on web app hosted at http://sample-site/friends/{friend-id}
. However when I try to use my contact resource on that page, the url to the resource is appended to the current URL:
GET | 的http://样品现场/朋友/ 1 / API /联系
但我真正需要的是让| 的http://采样网站/ API /联系
在该网页上的资源
but what I really need is to GET | http://sample-site/api/contact
on that resource on that page.
有没有配置资源来定义其他不同的基本URL不是相对于当前页面的方式?
Is there a way to configure a resource to define a different base URL other than in relation to the current page?
这是我试图改变内部资源基地网址:
This is my attempt to change the base URL inside the resource:
.factory('ContactRest', ['$resource', '$location', function($resource, $location) {
var host = $location.host()
return $resource('https://:url/:action', {}, {
// Normal CRUD actions
query: {
url : host,
action : 'api/contact',
method : 'GET',
isArray : true,
}
}]);
这使得 https://开头取样现场/朋友/ 1 /采样网站/ API /联系
。不管我如何定义$资源,只是不断追加任何的URL到基础URL。我需要改变URL的非常基地。
Which makes https://sample-site/friends/1/sample-site/api/contact
. Regardless of how I define the $resource, just keeps appending any URL's to the base URL. I need to change the very base of the URL.
推荐答案
我知道了!
.factory('ContactRest', ['$resource', '$location', function($resource, $location) {
var host = $location.host()
return $resource('http://' + host + ':action', {}, {
// Normal CRUD actions
query: {
method : 'GET',
isArray : true,
}
}]);
/**
* In controller
*/
ContactRest.query({ action: 'api/contact' }, function(contact) {
console.log('Got it, take that Havascript!');
});
这篇关于更改基本URL为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!