6

I am trying to use an environment variable in my angular.json file. I suspect this isn't possible, but thought I would see if anyone knew how. I am using it to point to a signalr file generated by the server (issue being when in dev vs. staging it's a different baseUrl). So... angular.json

"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ab-inbev-web2": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.json",
            "assets": [
              "src/assets",
              "src/favicon.ico"
            ],

            "scripts": [
              "node_modules/bootstrap/dist/js/bootstrap.min.js",
              "http://site-dev.com/signalr/hubs"
and when on staging
              "http://site-stage.com/signalr/hubs"
            ]

environment.ts

export const environment = {
  baseUrlWithSlash: 'http://site-prod.com/',
  logoffWarningTimeoutSeconds: 600,
  production: true,
  searchDelimiters: /[,;\n]+/
};

environment-local.ts
  export const environment = {
      baseUrlWithSlash: 'http://site-dev.com/',
      logoffWarningTimeoutSeconds: 600,
      production: true,
      searchDelimiters: /[,;\n]+/
    };

ect...

I am trying to avoid hard coding it and trying to have it work while running local. I thought maybe there is a way to sniff the location in the angular.json file and do an if statement.

13
  • @user184994 is a way to sniff the location in the angular.json file and do an if statement to append to the script location? Commented Nov 6, 2018 at 18:46
  • When you do a build, you pass the environment, such as ng build --prod for example. You can use environments I guess? github.com/angular/angular-cli/wiki/… Commented Nov 6, 2018 at 18:48
  • Since when JS has access to env variables??:> Commented Nov 6, 2018 at 18:50
  • @Antoniossss It doesn't, but Angular has an environments.ts file, which may provide the functionality that OP is after. Commented Nov 6, 2018 at 18:51
  • idk, he asked about ENV . Obviously environments is what he is looking for. Commented Nov 6, 2018 at 18:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.