要从ngForm的.ts文件中获取电子邮件值,可以使用以下解决方法:
import { Component, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent {
@ViewChild('myForm') myForm: NgForm;
emailValue: string;
getEmailValue() {
this.emailValue = this.myForm.value.email;
console.log(this.emailValue);
}
}
在上面的示例中,我们使用ViewChild装饰器获取了对ngForm的引用,并声明了一个名为emailValue的成员变量来保存电子邮件值。
当点击按钮时,getEmailValue方法将从ngForm中获取电子邮件值,并将其保存在emailValue变量中,并在控制台打印出该值。
请注意,上述示例假设您已经正确导入了FormsModule,并将其添加到您的模块中。