2017-04-10 22 views
-1

我想將Url存儲在配置文件中,所以當我部署在測試服務器或生產上時,我只需要更改配置文件中的url而不是js文件,但我不知道如何使用角度js中的配置文件配置文件角js

+0

[在AngularJS配置文件]中可能的複製(http://stackoverflow.com/questions/17876439/configuration-file-in-angularjs) –

回答

2

您可以使用angular.constant進行配置。

app.constant('appConfigurations', { 
    link_url: "http://localhost:8080/api", 
    //For production u can simply change the link_url 
    //link_url: "http://production_url/api" 
}); 
+0

如果有用,請接受答案 – Srigar

0

有可以對付它的方法,但在未來我們實現我們用來做下列方式

  1. 創建外部環境的js文件

    (function (window) { 
        window.__env = window.__env || {}; 
    
        window.__env.apiUrl = 'your Api Url'; 
    
    }(this)); 
    

  2. 在您的Index.html

添加app.js上述env.js

<!-- Load environment variables --> 
<script src="env.js"></script> 
  • 在你app.js

    變種envr = {}; (window){object.assign(envr,window.__ env) } //定義AngularJS應用程序var app = angular.module('myapp',[]);

    //將AngularJS中的環境註冊爲常量app.constant('__ env', env);

    更新:

  • 對於配置文件添加額外的網址:

    (function (window) { 
          window.__env = window.__env || {}; 
    
          window.__env.apiUrl = 'your Api Url'; 
    
         //Another Url 
         window.__env.baseUrl ='/'; 
    
         }(this)); 
    
    +0

    如果我想在外部環境文件中存儲不同的url,那我該怎麼辦? T' – Leena

    +0

    @Leena檢查我的更新 – Webruster

    +0

    非常感謝你 – Leena