2016-08-04 70 views
2

我正在登錄頁面上工作。我想要做的是在類validateLogin()submit()中有兩個功能。當點擊登錄按鈕我已成功提交,我可以得到用戶名和密碼。如何在angular2中的同一控制器內的另一個函數中調用一個函數

我想在我的提交函數中調用我的validateLogin函數,但我不能稱之爲我的崇高文本在validateLogin上顯示錯誤,並且在我的chrome瀏覽器中沒有收到任何錯誤。

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); 
    } 

} 

如何在我的提交函數中調用我的函數validateLogin?

下面是一個demo plunker它做工不錯

錯誤:

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'?

上面的錯誤我正在調用任何幫助

回答

3

由於您正在使用實例class,如果要引用該實例中的屬性或方法,則必須在屬性或方法名稱前面添加this.

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'?

這是因爲如果this不添加到validateLogin方法,使用該名稱的全局方法將被尋找,因爲它不存在,則會引發錯誤。所以,你的submit方法應該是:

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.

那是因爲你的validateLogin方法期望只是一個單一的參數(cb參數)和你發送三個參數(電子郵件,密碼和回撥)。

嘗試通過修改一行:

// 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); 
} 
+0

是的,我嘗試'this.'和我得到像'TypeScript錯誤:/ home/panini /AutoSparParts/app/pages/home/home.ts(40,5):錯誤TS2346:提供的參數不匹配調用目標的任何簽名。 ' –

2

要訪問類的成員,你需要之前的任何使用this.班級成員,在你的情況下試試這個

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

在你,而你的問題你使用的打字稿使用JavaScript您plunker演示,它們是不同的語言 –

+0

是的,我想獲得像一些事情那個javaScript plunker。我嘗試使用this.validateLogin,但它仍然是錯誤的。 –

+0

什麼是錯誤,如果可能請更新您的問題與更多的信息 –

相關問題