在Angular 8中,可以通过以下步骤从方法获取材料输入:
mat-form-field
和mat-input
来创建输入框:
export class MyComponent {
name: string;
constructor() {}
getName(): string {
return this.name;
}
}
export class MyComponent {
name: string;
constructor() {}
getName(): string {
return this.name;
}
getInput(): void {
const input = this.getName();
console.log(input);
}
}
这样,当用户在输入框中输入名称并点击按钮时,将通过getName()
方法获取输入的名称,并在控制台中打印出来。
请注意,上述示例中使用了双向数据绑定[(ngModel)]
来实现输入框的值和组件属性name
之间的同步更新。确保在组件的app.module.ts
文件中导入并在imports
数组中添加FormsModule
和MatInputModule
:
import { FormsModule } from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
@NgModule({
imports: [
FormsModule,
MatInputModule
],
...
})