File
Implements
Metadata
| selector |
app-action |
| styleUrls |
./action.component.scss |
| templateUrl |
./action.component.html |
Methods
|
onValueChanged
|
onValueChanged(data?: any)
|
|
|
Parameters :
| Name |
Type |
Optional |
| data |
any
|
Yes
|
|
|
Public
data
|
Type : any
|
Decorators :
@Inject(MAT_DIALOG_DATA)
|
|
|
|
formErrors
|
Type : object
|
Default value : {
title: '',
url: '',
description: '',
image_url: ''
}
|
|
|
|
validationMessages
|
Type : object
|
Default value : {
title: {
required: ''
},
url: {
required: '',
url: '请输入正确的链接'
},
description: {
required: ''
},
image_url: {
required: ''
}
}
|
|
|
import { Component, OnInit, Inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
@Component({
selector: 'app-action',
templateUrl: './action.component.html',
styleUrls: ['./action.component.scss']
})
export class ActionComponent implements OnInit {
actionFrom: FormGroup;
formErrors = {
title: '',
url: '',
description: '',
image_url: ''
};
validationMessages = {
title: {
required: ''
},
url: {
required: '',
url: '请输入正确的链接'
},
description: {
required: ''
},
image_url: {
required: ''
}
};
constructor(
@Inject('NavigationService') private service,
@Inject(MAT_DIALOG_DATA) public data: any,
private fb: FormBuilder
) {
this.createForm();
}
ngOnInit() {}
createForm() {
this.actionFrom = this.fb.group({
// id: this.data.id,
title: [this.data.title, [Validators.maxLength(32)]],
url: this.data.link,
description: this.data.description,
image_url: this.data.image_link || 'assets/logo.png'
});
this.actionFrom.valueChanges.subscribe(data => this.onValueChanged(data));
this.onValueChanged();
}
onSubmit() {
console.log(this.actionFrom.value);
this.service.update(this.data.id, this.actionFrom.value);
}
onValueChanged(data?: any) {
if (!this.actionFrom) {
return;
}
const form = this.actionFrom;
for (const field in this.formErrors) {
if (Object.prototype.hasOwnProperty.call(this.formErrors, field)) {
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
if (Object.prototype.hasOwnProperty.call(control.errors, key)) {
this.formErrors[field] += messages[key] + ' ';
}
}
}
}
}
}
}
<form [formGroup]="actionFrom" (ngSubmit)="onSubmit()">
<mat-dialog-content>
<mat-form-field class="display-block">
<input type="text" matInput placeholder="标题" formControlName="title" required>
<mat-error *ngIf="formErrors.title">{{formErrors.title}}</mat-error>
</mat-form-field>
<mat-form-field class="display-block">
<input type="text" matInput placeholder="链接" formControlName="url" required>
<mat-error *ngIf="formErrors.url">{{formErrors.url}}</mat-error>
</mat-form-field>
<mat-form-field class="display-block">
<textarea matInput placeholder="介绍" formControlName="description"></textarea>
<mat-error *ngIf="formErrors.description">{{formErrors.description}}</mat-error>
</mat-form-field>
<mat-form-field class="display-block">
<input type="text" matInput placeholder="logo" formControlName="image_url">
<mat-error *ngIf="formErrors.image_url">{{formErrors.image_url}}</mat-error>
</mat-form-field>
</mat-dialog-content>
<mat-dialog-actions fxLayout="row" fxLayoutAlign="space-between center">
<button mat-raised-button type="reset" mat-dialog-close>取消</button>
<button mat-raised-button color="primary" type="submit" [disabled]="!actionFrom.valid">提交</button>
</mat-dialog-actions>
</form>
.display-block {
display: block;
}
Legend
Html element with directive