相关疑难解决方法(0)

如何根据获取的环境变量动态设置AngularjJS基本URL?

我有一个开发和生产环境,我的URL有所不同:

生产:

www.exmaple.com/page

发展:

dev.environment/project/page

我知道我可以在AngularJS中设置基本URL

<base href='/project/' />

但这对我没有帮助.在我加载AngularJS应用程序之前,我获取一个配置文件(在app.js中,带有.run语句,该语句读取具有环境的变量:

]).run([
  '$rootScope',
  '$http',
  function (
    $rootScope,
    $http
  ) {
    var configDeferred = $q.defer();

    // fetch config and set the API    
    $http.get('config.json').then(function(response) {
      $rootScope.config = response.data;
      configDeferred.resolve();
    });

    // Wait for the config to be loaded, then go to state
    $rootScope.$on('$stateChangeStart', function (event, next, current) {
      event.preventDefault();
      $q.all([configDeferred.promise]).then(function() {
        $state.transitionTo(next.name);
        return;
      });
    });
Run Code Online (Sandbox Code Playgroud)

有没有办法根据AngularJS中提取的配置文件(可能带有.htaccess)动态设置基本URL?

尝试1: 尝试.run通过ng-href 获取配置并设置基本URL:

在我的app.js中编辑以下代码行:

// fetch config and set the API    
$http.get('config.json').then(function(response) { …
Run Code Online (Sandbox Code Playgroud)

environment .htaccess config base-url angularjs

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

.htaccess ×1

angularjs ×1

base-url ×1

config ×1

environment ×1