具有angularjs的环境特定配置

时间:2015-01-07 16:43:45

标签: angularjs asp.net-web-api

我正在使用Angularjs和WebAPI创建一个网站,作为Visual Studio的后端支持。

我已将app.config.js中的webapi网址定义为应用常量:

    var serviceBase = 'http://localhost/Webapiservice/';
    app.constant('ngAppSettings', {
        apiServiceBaseUri: serviceBase,
        clientId: 'ngTestApp'
    });

现在,对于QA环境(http://QAServer/Webapiservice/),webapi位于不同的URL,我们的生产环境(http://ProdServer/Webapiservice/)也是如此。

我可以手动将js文件更新到适当的位置。但有没有办法自动化这个过程,所以webAPI指向正确的URL?

有可能用grunt做到这一点吗?我以前从未使用过咕噜声。

2 个答案:

答案 0 :(得分:0)

ngConstant和grunt一起做得很好:https://github.com/werk85/grunt-ng-constant。通过这种方式,您可以将您的环境指定为JSON文件,并且在编译/运行时,grunt会生成一个带有模块的enviornment.js文件(我称之为我的ENV),这可以在您的每个部分中注入应用

答案 1 :(得分:0)

我会做这样的事情:

angular
  .module('app', [])
  .value('$path', {
      rest : "http://localhost/Webapiservice"
  })

你会打这样的话:

app.constant('ngAppSettings', {
    apiServiceBaseUri: $path.rest,
    clientId: 'ngTestApp'
});
相关问题