2015-02-05 69 views
3

我正在使用Angular在同一应用程序上使用RESTful API。我在http://sample-site.com/api/contacts更改资源的基本URL

这是伟大的,它的工作原理为contacts资源$资源建立,但是我需要用的/api/contacts上的应用程序的不同页面的基本CRUD交互。

例如,我需要在位于http://sample-site/friends/{friend-id}的Web应用程序的另一个页面再次与联系人进行交互。但是,当我尝试在该页面上使用我的联系人资源时,资源的URL被附加到当前的 URL:

GET | http://sample-site/friends/1/api/contact

但我真正需要的是GET |该页面上的该资源上的http://sample-site/api/contact

有没有办法配置一个资源来定义一个不同于当前页面的基础URL?

这是我试图改变内部资源的基础网址:

.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://sample-site/friends/1/sample-site/api/contact。无论我如何定义$资源,只要将任何网址附加到基本网址即可。我需要更改URL的基础。

回答

6

我明白了!

.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!'); 
});