Configuration file in AngularJS

前端 未结 3 473
心在旅途
心在旅途 2020-12-07 11:05

What is the best way to create config file (Something like web config in .net), for storing urls, and other constants that may vary during the application deploy?

3条回答
  •  失恋的感觉
    2020-12-07 11:42

    Thinking outside the box, you don't really want to use .constant as it's tied to the application. Use a config that sits outside of the app and you have more control over env configs. I have provided a link below explains the pitfalls of have configs within the angular build itself.

    (function hydrateConfiguration() {
    "use strict";
    var xhr = new XMLHttpRequest();
    xhr.open("get", "conf.json", window);
    xhr.onload = function () {
        var status = xhr.status;
        if (status === 200) {
            if (xhr.responseText) {
                var response = JSON.parse(xhr.responseText);
                window.ss_conf = response;
            }
        } else {
    
            console.error("messages: Could not load confguration -> ERROR ->", status);
        }
    };
    
    xhr.send() )());
    

    Just a simple example where a external config file controls the state of the app and injects values outside in, instead of inside in.

    https://www.jvandemo.com/how-to-configure-your-angularjs-application-using-environment-variables/

提交回复
热议问题