0

I have two environments, one for staging and another for production. They both have different URLs and I want to build my project using Angular CLI. Right now I have to comment one URL and run another. But I want to control these things directly from the environment file.

I used this way (from Different proxy config based on environment in Angular 2 CLI):

// environment.ts
export const environment = {
    production: false,
    api: 'http://localhost:3000'
};

// environment.prod.ts
export const environment = {
    production: true,
    api: 'http://api.exampledomain.com'
}

Then in your ts source files pull the domain from the environment file:

// some service
import { Injectable } from '@angular/core';
import { environment } from '../../../environment.ts';
import { Http } from '@angular/http';

@Injectable()
export class SomeService {
    constructor(private http: Http);

    getData(){
        return this.http.get(environment.api + '/rest-of-api');
    }
}

But in this if I want to build from production how do I change environment variable value? Because here I import from environment.ts, not environment.prod.ts.

5
  • See: github.com/angular/angular-cli/wiki/build Commented Oct 24, 2017 at 10:55
  • Yes i have read this but it does not worked in my case. Commented Oct 24, 2017 at 10:58
  • Then show a minimal reproducible example of that. What precisely do you mean by "not worked"? I have used this feature, and not had any problems with it. Commented Oct 24, 2017 at 10:59
  • How to i define two environment file. So that it will take required file in services Commented Oct 24, 2017 at 11:03
  • Exactly the way you have above, as indeed they are set up by the CLI. Read the comments in environment.ts. You don't need to change anything in the service, that's the whole point. Commented Oct 24, 2017 at 11:06

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.