【发布时间】:2019-05-11 12:29:13
【问题描述】:
我有一个 stackblitz 作为指导。
我想显示我单击“编辑”按钮的材料卡列表,我可以在其中编辑文本字段,当我单击“保存”图标时,它当然会通过触发功能等来保存.
然而,我正在努力掌握这一切在 Angular 中的工作原理以及我的应用的 Material 特性。
html
<form id="myForm" [formGroup]="thisIsMyForm">
<mat-card [formGroup]="x" *ngFor="let x of data; let i = index">
<mat-form-field>
<label for="{{x.name}}">Name</label>
<input formControlName="name" id="{{x.name}}" matInput value="{{x.name}}">
</mat-form-field>
<mat-form-field>
<label for="{{x.type}}">Type</label>
<input formControlName="type" id="{{x.type}}" matInput value="{{x.type}}"/>
</mat-form-field>
</mat-card>
</form>
ts
import { Component, ViewChild } from '@angular/core';
import {MatSnackBar} from '@angular/material';
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
thisIsMyForm: FormGroup
data = [
{name:"one", type:"one"},
{name:"two", type:"two"},
{name:"three", type:"three"},
];
constructor(private formBuilder: FormBuilder) {}
onSubmit() {
// Here I would like to be able to access the values of the 'forms'
}
}
【问题讨论】:
-
因为它是
ngFor,您可以使用FormArray或QuerySelector来执行此操作。选择权在你。 -
@JacopoSciampi 你能指导我举个例子吗?
-
我会做一个stackblitz
标签: html angular typescript angular-material