typescript 如何在angular2的同一控制器内的另一个函数中调用一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38761388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call one function in another function inside same controller in angular2
提问by Mohan Gopi
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.
我正在登录页面上工作。我想要做的是我在一个类中有两个函数validateLogin()
和submit()
. 单击登录按钮时,我已成功提交,并且可以获取用户名和密码。
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.
我试图在我的提交函数中调用我的 validateLogin 函数,但我无法调用它,我的 sublime 文本在 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);
}
}
How can i call my function validateLogin in my submit function ?
如何在提交函数中调用我的函数 validateLogin ?
Here is an demo plunkerwhere it works good
这是一个演示 plunker,它运行良好
Error:
错误:
GET http://localhost:8100/build/js/app.bundle.jsin 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'?
在我的控制台中获取http://localhost:8100/build/js/app.bundle.js。
在我的服务中,我收到错误消息
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
上述错误我正在调用任何帮助
采纳答案by sebaferreras
Since you're working with an instanceof 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.
既然你用工作实例的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'?
在我的服务中,我收到类似 TypeScript 错误的错误:/home/panini/AutoSparParts/app/pages/home/home.ts(40,5):错误 TS2663:找不到名称“validateLogin”。您是说实例成员“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 submit
method should be:
那是因为如果不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.
是的,我试试这个。并且我收到类似 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).
那是因为您的validateLogin
方法只需要一个参数(cb
参数),而您要发送三个参数(电子邮件、密码和回调)。
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);
}
回答by Ankit Singh
To access class members you need to use this.
before any class member , in your case try this
要访问您需要this.
在任何类成员之前使用的类成员,在您的情况下试试这个
this.validateLogin(this.loginForm.value.email, this.loginForm.value.password, (response) => {
console.log(response);
this.commentOnTimeOut = response;
})