【问题标题】:Restangular does not work with relative URLRestangular 不适用于相对 URL
【发布时间】:2014-10-21 12:31:50
【问题描述】:

我正在使用 angular.js,并且我有一个使用 Web 服务的模块

/* 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;
}
]);

【问题讨论】:

  • 您使用的是什么版本的 Restangular? RestangularProvider.setBaseUrl('/ig-web/services');是一个正确的电话。我从未见过使用 './' 作为 url,但您提到的第一个选项应该可以正常工作。您是否收到错误消息?它在什么方面不起作用?

标签: angularjs restangular


【解决方案1】:

好的,希望对遇到同样问题的人有所帮助。

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

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

因此,如果我允许使用相对路径,我的 Web 服务将无法使用 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;
}
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2019-03-27
    • 2015-08-04
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多