1

In development, I use http://localhost:565565/api/v2 in an angular service to retrieve data from local server.

Now in production, I want to change that url to https://www.data.com/api/v2.

How to change with angluar cli ? I don't want manually change the big main.xxxxxxx.js file.

Until now I use ng build --prod but it is not enough.

1
  • I know it is likely typo, but does your service really run on that port? I didn't think they could be that big. Commented Jul 17, 2018 at 13:16

2 Answers 2

3

Use the environments provided in angular.cli:

On environment.ts for development use

export const environment = {
    production: false,
    apiUrl: 'http://localhost:565565/api/v2'
};

On environment.prod.ts for production use:

export const environment = {
      production: true,
      apiUrl: 'https://www.data.com/api/v2'
};

Then depending on which environment you run your app the corresponding file will be used. If you run ng serve environment.ts will be used, if you run ng build --prod environment.prod.ts will be used.

Additionally you will need to import environment to your ts to use apiUrl

example in ts

import { environment } from './../environments/environment';

and use it as environment.apiUrl

Sign up to request clarification or add additional context in comments.

Comments

0

Use environment file:

ng build --environment=production

export const environment = {  
  production: true,
  apiUrl: "prod_Url"
};

export const environment = {  
      production: false,
      apiUrl: "dev_Url"
    };

Comments

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.