Mohan Gopi
Mohan Gopi

Reputation: 7724

How to call one function in another function inside same controller in angular2

I am working on login page. What I am trying to do is I have two function in a class validateLogin() and submit(). When clicking the login button my is successfully submitted and I am able to get the username and password.

I am trying to call my validateLogin function in my submit function but I am not able call it my sublime text shows an error on the validateLogin and I am not getting any error in my chrome Browser.

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {FormBuilder,Validators} from "@angular/common";

@Component({
  templateUrl: 'build/pages/home/home.html'
})

export class HomePage {

    public loginForm: any;
  public commentOnTimeOut: any;

  constructor(private navCtrl: NavController, public form:FormBuilder) {
    this.loginForm = this.form.group({
        "email":["",Validators.required],
        "password":["",Validators.required]
    })
  }

  validateLogin(cb){
    window.setTimeout( () => {
        if (this.loginForm.value.email == 'admin' && this.loginForm.value.password == 'admin123') {
          console.log("invoking If part");
          cb('valid User');
        }
        else {
          console.log("invoking elsePart");
          cb('Invalid User');
        }
    }, 3000);
  }

  submit(){

    console.log(this.loginForm.value);
    console.log(this.loginForm.value.email);
    console.log(this.loginForm.value.password);

    validateLogin(this.loginForm.value.email, this.loginForm.value.password, (response) => {
      console.log(response);
      this.commentOnTimeOut = response;
    })

    this.commentOnTimeOut = "Validating User";
    console.log(this.commentOnTimeOut);
  }  

}

How can i call my function validateLogin in my submit function ?

Here is an demo plunker where it works good

Error:

GET http://localhost:8100/build/js/app.bundle.js in my console.

In my serve i get error like TypeScript error: /home/panini/AutoSparParts/app/pages/home/home.ts(40,5): Error TS2663: Cannot find name 'validateLogin'. Did you mean the instance member 'this.validateLogin'?

the above error i am invoking any help

Upvotes: 3

Views: 21745

Answers (2)

sebaferreras
sebaferreras

Reputation: 44659

Since you're working with an instance of a class, if you want to refer to a property or method inside that instance, you have to add the this. before the property or method name.

In my serve i get error like TypeScript error: /home/panini/AutoSparParts/app/pages/home/home.ts(40,5): Error TS2663: Cannot find name 'validateLogin'. Did you mean the instance member 'this.validateLogin'?

That's because if don't add the this to the validateLogin method, a global method with that name will be looked for, and because it's not there, an error is thrown. So your submitmethod should be:

submit(){

    console.log(this.loginForm.value);
    console.log(this.loginForm.value.email);
    console.log(this.loginForm.value.password);

    this.validateLogin(this.loginForm.value.email, this.loginForm.value.password, (response) => {
      console.log(response);
      this.commentOnTimeOut = response;
    })

    this.commentOnTimeOut = "Validating User";
    console.log(this.commentOnTimeOut);
  } 

UPDATE:

yes i try that this. and i get error like TypeScript error: /home/panini/AutoSparParts/app/pages/home/home.ts(40,5): Error TS2346: Supplied parameters do not match any signature of call target.

That's because your validateLogin method expect just a single parameter (the cb parameter) and you're sending three parameters (email, password and the callback).

Try by modifying that line like this:

 // New method to handle the response
 processLoginResult(response) {
    console.log(response);
    this.commentOnTimeOut = response;
 }

 submit(){

    console.log(this.loginForm.value);
    console.log(this.loginForm.value.email);
    console.log(this.loginForm.value.password);

    // Instead of calling it with email, password and the callback
    // just send the added method as a callback 
    this.validateLogin(this.processLoginResult);

    this.commentOnTimeOut = "Validating User";
    console.log(this.commentOnTimeOut);
}

Upvotes: 3

Ankit Singh
Ankit Singh

Reputation: 24945

To access class members you need to use this. before any class member , in your case try this

this.validateLogin(this.loginForm.value.email, this.loginForm.value.password, (response) => {
   console.log(response);
   this.commentOnTimeOut = response;
})

Upvotes: 3

Related Questions