如何在angular 8中从表单中获取数据
我想打印在文本中输入的数据,并在console中使用下拉列表。我不知道如何捕获这些数据。为两个字段创建两个变量,并在表单字段中使用[ngModel] TS: HTML:如何在angular 8中从表单中获取数据,angular,Angular,我想打印在文本中输入的数据,并在console中使用下拉列表。我不知道如何捕获这些数据。为两个字段创建两个变量,并在表单字段中使用[ngModel] TS: HTML: 当您想在控制台中打印该数据时,为什么要打印?请尝试这种方法。请阅读文档,首先决定是使用模板还是被动:。。。或者只使用[ngModel]而不使用表单。我建议回复:@Binara Thambugala无法在控制台中打印任何内容。实际上,如果我只更改名字,并且在提交表单后保持下拉列表不变,则我正在使用此表单进行更新操作。下拉列表显示的
当您想在控制台中打印该数据时,为什么要打印?请尝试这种方法。请阅读文档,首先决定是使用模板还是被动:。。。或者只使用[ngModel]而不使用表单。我建议回复:@Binara Thambugala无法在控制台中打印任何内容。实际上,如果我只更改名字,并且在提交表单后保持下拉列表不变,则我正在使用此表单进行更新操作。下拉列表显示的值未定义,即使我在下拉列表中有值,它也没有打印任何内容,或者我必须使用任何内容单击“输入”按钮您必须将值分配给第一个\u名称和角色,然后它将打印出来。例如,当您编辑time assign first_name=data['user_first_name']的字段时;角色=已选择
<form ngSubmit="loginUser()">
<div class="row">
<div class="col-md">
<mat-form-field class="example-full-width" >
<input matInput type="text" name="user_first_name" placeholder="First Name" value="
{{data['user_first_name']}}">
<button mat-button matSuffix mat-icon-button aria-label="First Name" (click)="firstname=''">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
</div>
<div class="row">
<div class="col-md">
<mat-form-field>
<mat-select [(value)]="selected" placeholder="Role">
<mat-option value="User">User</mat-option>
<mat-option value="Viewer">Viewer</mat-option>
</mat-select>
</mat-form-field>
</div>
first_name: any;
role: any;
<form ngSubmit="loginUser()">
<div class="row">
<div class="col-md">
<mat-form-field class="example-full-width" >
<input matInput type="text" name="user_first_name" placeholder="First Name" value="
{{data['user_first_name']}}" [(ngModel)]="first_name" >
<button mat-button matSuffix mat-icon-button aria-label="First Name" (click)="firstname=''">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
</div>
<div class="row">
<div class="col-md">
<mat-form-field>
<mat-select placeholder="Role" [(ngModel)]="role">
<mat-option value="User">User</mat-option>
<mat-option value="Viewer">Viewer</mat-option>
</mat-select>
</mat-form-field>
</div>
loginUser() {
console.log(this.first_name);
console.log(this.role);
}