要使用Angular映射使用对象,您可以按照以下步骤进行操作:
export class User {
name: string;
age: number;
}
@ViewChild
装饰器来获取对应的HTML元素。import { Component, ViewChild } from '@angular/core';
import { User } from './user.model';
@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent {
@ViewChild('nameInput') nameInput: any;
@ViewChild('ageInput') ageInput: any;
user: User = new User();
onSubmit() {
// 在这里进行对象映射
this.user.name = this.nameInput.nativeElement.value;
this.user.age = this.ageInput.nativeElement.value;
}
}
在这个例子中,我们使用[(ngModel)]
指令将输入框与user.name
和user.age
属性进行绑定。我们还使用#nameInput
和#ageInput
来获取输入框的引用。
user
对象的相应属性。在示例中,我们使用nativeElement.value
来获取输入框的值。这就是如何在Angular中使用对象映射来处理表单输入。当您提交表单时,user
对象将包含输入框的值。您可以根据需要对其进行进一步处理。