Pass an html ngModel value to service

654 views Asked by At

I want to pass an html ngModel value <input type="email" [(ngModel)]="email"/> which is located on app.component.html, to a service which is called email.service.ts. Inside the service I have this code:

const apiEmail ='http://apilayer.net/api/check?access_key=key' +'&email=' +emailAddress;

I want to dynamically pass the [(ngModel)]="email" value to +emailAddress so every time the user inputs an email adress to be able to check if it exists or not.

Thank you!

2

There are 2 answers

0
Clemens Sum On

You could easily define a setter for the email property in your component and call the required service-method there.

Component

private _email: string;

set email(email: string) {
  this._email = email;
  this.emailService.checkEmail(email);
}

get email() {
  return this._email;
}
1
M Danial On

Change emailAddress to email

Thanks Danial