Restangular不适用于相对URL

时间:2014-10-21 12:31:51

标签: angularjs restangular

我正在使用angular.js,我有一个使用网络服务的模块

/* Rest WS Config */
RestangularProvider.setBaseUrl(ENVL.apiEndpoint+'/ig-web/services');

工作正常。

我想使用相对的方式来设置我的基本URL(因为我不能为每个环境提供特定的配置)所以我试过了:

RestangularProvider.setBaseUrl('/ig-web/services');
RestangularProvider.setBaseUrl('./ig-web/services');

但那没用。

有什么想法吗?

以下是完整的模块文件

'use strict';

angular.module('env.config', [])
.constant('ENVL', {
    'name': 'development local',
    'apiEndpoint': 'http://localhost:8080'
})
.constant('DEVT', {
    'name': 'development server',
    'apiEndpoint': 'http://domain.dev'
})
.constant('INT', {
    'name': 'integration server',
    'apiEndpoint': 'http://domain.int'
})
.constant('REC', {
    'name': 'recette server',
    'apiEndpoint': 'http://domain.rec'
});


var app = angular.module('saisinesApp', [
'ngAnimate',
'ngResource',
'ui.bootstrap',
'toaster',
'angular.css.injector',
'angularFileUpload',
'dialogs',
'ui.router.state',
'ajoslin.promise-tracker',
'cgBusy',
'restangular',
'ngGrid',
'xeditable',
'env.config',
'saisinesAppFilters'])
.constant('_', window._)
.constant('jsPDF',window.jsPDF);

 app.config(function ($stateProvider, $urlRouterProvider, RestangularProvider,ENVL) {
/* Rounting config*/
$urlRouterProvider.when('/home', '/home/search');

$stateProvider
    .state('auth', {
        url: '/',
        templateUrl: 'src/auth/authentication.html',
        controller: 'AuthCtrl'
    })
    .state('auth-by-url', {
        url: '/:token',
        templateUrl: 'src/auth/authentication.html',
        controller: 'AuthCtrl'
    })
    .state('home', {
        abstract: true,
        url: '/home',
        templateUrl: 'src/common/header.html',
        controller: 'HeaderCtrl'
    })
    .state('home.search', {
        url: '/search',
        templateUrl: 'src/search/search.html',
        controller: 'SearchCtrl'
    })
    .state('home.search-by-siren', {
        url: '/search/:siren',
        templateUrl: 'src/search/search.html',
        controller: 'SearchCtrl'
    })
    .state('home.create', {
        url: '/create',
        templateUrl: 'src/create/create.html',
        controller: 'CreateCtrl'
    })
    .state('home.recap', {
        url: '/recap',
        templateUrl: 'src/recap/recap.html'
    });

   $urlRouterProvider.otherwise('/');

 /* Rest WS Config */
//RestangularProvider.setBaseUrl(ENVL.apiEndpoint+'/ig-web/services');
 RestangularProvider.setBaseUrl('./ig-web/services');
 });

 app.run([
'$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
}
]);

1 个答案:

答案 0 :(得分:1)

好的,我希望如果他们遇到同样的问题,它会有所帮助。

当我在本地测试时,我使用的是带有端口8000的grunt服务器

但我的网络服务位于端口8080(对应于ENV常量)。

因此,如果我放弃相对路径,我的网络服务就无法使用grunt端口。

然而,当使用JBOSS和相对路径时,它起作用,因为现在相对路径指向正确的地址。

因此我的正确配置是:

'use strict';


 var app = angular.module('saisinesApp', [
'ngAnimate',
'ngResource',
'ui.bootstrap',
'toaster',
'angular.css.injector',
'angularFileUpload',
'dialogs',
'ui.router.state',
'ajoslin.promise-tracker',
'cgBusy',
'restangular',
'ngGrid',
'xeditable',
'saisinesAppFilters'])
.constant('_', window._)
.constant('jsPDF',window.jsPDF);

app.config(function ($stateProvider, $urlRouterProvider, RestangularProvider) {
/* Rounting config*/
$urlRouterProvider.when('/home', '/home/search');

$stateProvider
.state('auth', {
    url: '/',
    templateUrl: 'src/auth/authentication.html',
    controller: 'AuthCtrl'
 })
.state('auth-by-url', {
    url: '/:token',
    templateUrl: 'src/auth/authentication.html',
    controller: 'AuthCtrl'
 })
.state('home', {
    abstract: true,
    url: '/home',
    templateUrl: 'src/common/header.html',
    controller: 'HeaderCtrl'
})
.state('home.search', {
    url: '/search',
    templateUrl: 'src/search/search.html',
    controller: 'SearchCtrl'
})
.state('home.search-by-siren', {
    url: '/search/:siren',
    templateUrl: 'src/search/search.html',
    controller: 'SearchCtrl'
})
.state('home.create', {
    url: '/create',
    templateUrl: 'src/create/create.html',
    controller: 'CreateCtrl'
})
.state('home.recap', {
    url: '/recap',
    templateUrl: 'src/recap/recap.html'
 });

 $urlRouterProvider.otherwise('/');

/* Rest WS Config */
 RestangularProvider.setBaseUrl('/ig-web/services');
});

app.run([
'$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams) {
 $rootScope.$state = $state;
 $rootScope.$stateParams = $stateParams;
}
]);
相关问题