单击 MatFormFieldControl 中的按钮触发 Angular 中表单的提交操作 4
Click on a button in a MatFormFieldControl trigger the Submit action on Form in Angular 4
我开发了一个自定义组件 (app-mat-avatar) 用作头像选择(见图)。它由一个带有图片和 2 个按钮的大 div 组成。一个用于编辑,另一个用于擦除。
我按照 Creating a custom form field control 的 Angular 指南构建了它
这是我使用的表格:
<form name="detailsForm" [formGroup]="detailsForm" (submit)="submitDetails($event)">
<app-mat-avatar [size]="150" formControlName="photoURL"></app-mat-avatar>
<input matInput type="email" placeholder="Email" formControlNamesss="email" disabled value="{{detailsForm.get('email').value}}">
.....
<div flex layout="row" layout-align="end center" class="fullWidth pad-right-sm">
<button mat-button mat-raised (click)="resetDetails()" class="push-right-xs" [disabled]="busy">Reset</button>
<button type="submit" mat-button mat-raised-button color="primary" [disabled]="detailsForm.invalid || busy">Save</button>
</div>
</form>
这是支持表单所在页面组件的代码:
@Component({
selector: 'app-my-info',
templateUrl: './my-info.component.html',
styleUrls: ['./my-info.component.sass']
})
export class MyInfoComponent implements OnInit, OnDestroy {
detailsForm = new FormGroup({
uid: new FormControl(''),
photoURL: new FormControl(''),
firstName: new FormControl('', [Validators.required, Validators.minLength(2)]),
lastName: new FormControl('', [Validators.required, Validators.minLength(2)]),
sex: new FormControl('', [Validators.required]),
email: new FormControl('', [Validators.required, Validators.email]),
licenseLocal: new FormControl(''),
licenseNational: new FormControl(''),
cellPhone: new FormControl(''),
});
........
ngOnInit() {
fetchUserFromDatabase.subscribe( me => {
if (!me) {return; }
this._currentUser = me;
this.resetDetails();
}));
}
resetDetails() {
const user = {
email : this.currentUserCopy.email,
photoURL: this.currentUserCopy.photoURL,
firstName: this.currentUserCopy.firstName,
lastName: this.currentUserCopy.lastName,
licenseLocal: this.currentUserCopy.licenseLocal || '',
licenseNational: this.currentUserCopy.licenseNational || '',
sex: this.currentUserCopy.sex,
cellPhone: this.currentUserCopy.cellPhone || ''
};
this.detailsForm.patchValue( user );
}
submitDetails(event) {
// console.log(event);
this._currentUser.createFromFirebaseData(this.detailsForm.value);
this._db.UpdateUser(this._currentUser)
.then( result => {
this.busy = false;
this._popUp.showInfo('Perfil guardado');
})
.catch( error => {
this.busy = false;
this._popUp.showError(error);
});
}
}
一切正常,除了当我点击 app-mat-avatar 组件的按钮之一时,submitDetails
方法被触发。
我尝试在按钮点击调用的方法的第一行添加event.stopPropagation();
,但是没有效果。
知道为什么会这样吗?
将 type="button"
添加到模板中的重置按钮以防止提交
设置type="reset"
如下,
<button mat-button mat-raised type="reset" (click)="resetDetails()" class="push-right-xs" [disabled]="busy">Reset</button>
我开发了一个自定义组件 (app-mat-avatar) 用作头像选择(见图)。它由一个带有图片和 2 个按钮的大 div 组成。一个用于编辑,另一个用于擦除。
我按照 Creating a custom form field control 的 Angular 指南构建了它
这是我使用的表格:
<form name="detailsForm" [formGroup]="detailsForm" (submit)="submitDetails($event)">
<app-mat-avatar [size]="150" formControlName="photoURL"></app-mat-avatar>
<input matInput type="email" placeholder="Email" formControlNamesss="email" disabled value="{{detailsForm.get('email').value}}">
.....
<div flex layout="row" layout-align="end center" class="fullWidth pad-right-sm">
<button mat-button mat-raised (click)="resetDetails()" class="push-right-xs" [disabled]="busy">Reset</button>
<button type="submit" mat-button mat-raised-button color="primary" [disabled]="detailsForm.invalid || busy">Save</button>
</div>
</form>
这是支持表单所在页面组件的代码:
@Component({
selector: 'app-my-info',
templateUrl: './my-info.component.html',
styleUrls: ['./my-info.component.sass']
})
export class MyInfoComponent implements OnInit, OnDestroy {
detailsForm = new FormGroup({
uid: new FormControl(''),
photoURL: new FormControl(''),
firstName: new FormControl('', [Validators.required, Validators.minLength(2)]),
lastName: new FormControl('', [Validators.required, Validators.minLength(2)]),
sex: new FormControl('', [Validators.required]),
email: new FormControl('', [Validators.required, Validators.email]),
licenseLocal: new FormControl(''),
licenseNational: new FormControl(''),
cellPhone: new FormControl(''),
});
........
ngOnInit() {
fetchUserFromDatabase.subscribe( me => {
if (!me) {return; }
this._currentUser = me;
this.resetDetails();
}));
}
resetDetails() {
const user = {
email : this.currentUserCopy.email,
photoURL: this.currentUserCopy.photoURL,
firstName: this.currentUserCopy.firstName,
lastName: this.currentUserCopy.lastName,
licenseLocal: this.currentUserCopy.licenseLocal || '',
licenseNational: this.currentUserCopy.licenseNational || '',
sex: this.currentUserCopy.sex,
cellPhone: this.currentUserCopy.cellPhone || ''
};
this.detailsForm.patchValue( user );
}
submitDetails(event) {
// console.log(event);
this._currentUser.createFromFirebaseData(this.detailsForm.value);
this._db.UpdateUser(this._currentUser)
.then( result => {
this.busy = false;
this._popUp.showInfo('Perfil guardado');
})
.catch( error => {
this.busy = false;
this._popUp.showError(error);
});
}
}
一切正常,除了当我点击 app-mat-avatar 组件的按钮之一时,submitDetails
方法被触发。
我尝试在按钮点击调用的方法的第一行添加event.stopPropagation();
,但是没有效果。
知道为什么会这样吗?
将 type="button"
添加到模板中的重置按钮以防止提交
设置type="reset"
如下,
<button mat-button mat-raised type="reset" (click)="resetDetails()" class="push-right-xs" [disabled]="busy">Reset</button>