Monday, October 3, 2022
HomeError SolveHow to make API Calls the Right Way in Angular?

How to make API Calls the Right Way in Angular?

Step 1) Create a Common folder in your application.

export class Config {
  apiURL = 'https://localhost:44328/';
}

Step 2) In your module.ts file inject the class.

providers: [Security, MyAuthGuard, Config],

Step 3) Open your component.ts file. Inject the Config file in your component constructor.

export class AddComponent implements OnInit {
  constructor(public config: Config) {}

Step 4) In the same component write the code below, here in my code it is ADD METHOD.

AddRecord() {
    var observable = this.httpClient.post(
      this.config.apiURL + 'Register/AddUser',
      this.userObj
    );
    observable.subscribe(
      (res) => this.Sucess(res),
      (res) => this.Error(res)
    );
  }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments