要解决“Angular按钮点击没有获取到表单值”的问题,可以按照以下步骤进行:
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent {
formValue: string;
submitForm() {
console.log(this.formValue);
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { FormComponent } from './form/form.component';
@NgModule({
declarations: [
AppComponent,
FormComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
通过以上步骤,点击按钮时应该能够正确获取到表单的值。确保在组件中正确定义变量,并在模板中正确绑定表单值。同时,确保在模块中正确导入FormsModule。