Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 更改资源的基本URL_Angularjs_Angular Resource - Fatal编程技术网

Angularjs 更改资源的基本URL

Angularjs 更改资源的基本URL,angularjs,angular-resource,Angularjs,Angular Resource,我使用Angular在同一个应用程序上使用RESTful API。我在http://sample-site.com/api/contacts 这很好,而且很有效,但是我需要在应用程序的不同页面上与/api/contacts的基本CRUD交互 例如,我需要在位于http://sample-site/friends/{friend id}。但是,当我尝试在该页面上使用我的联系人资源时,该资源的url会附加到当前的url中: 获取|http://sample-site/friends/1/api/co

我使用Angular在同一个应用程序上使用RESTful API。我在
http://sample-site.com/api/contacts

这很好,而且很有效,但是我需要在应用程序的不同页面上与
/api/contacts
的基本CRUD交互

例如,我需要在位于
http://sample-site/friends/{friend id}
。但是,当我尝试在该页面上使用我的联系人资源时,该资源的url会附加到当前的url中:

获取|
http://sample-site/friends/1/api/contact

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

是否有方法配置资源以定义与当前页面相关的其他基本URL

这是我试图更改资源内的基本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
。不管我如何定义$resource,只需将任何URL附加到基本URL即可。我需要更改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!');
});