带有排序列表的示例 CRUD:https://sorting-list-angular.web.app/library
整个应用的Git:https://bitbucket.org/mises543/sorting-list/src/master/
这是一个简单的应用程序,其中的 NGXS 有点矫枉过正,但我正在学习。它是数据状态管理。它被称为 Redux。
你可以学习一下。
模板:
<div class="add-item-container">
<mat-label>
<h2>Add Media</h2>
</mat-label>
<section>
<mat-form-field>
<input matInput placeholder="Enter title:" type="text" [formControl]='title' required>
<mat-error *ngIf="title.invalid && !''">{{getErrorTitle()}}</mat-error>
</mat-form-field>
<mat-form-field>
<mat-select matInput placeholder="Category:" [formControl]="category" required>
<ng-container *ngFor="let category of categories">
<mat-option *ngIf="category != 'All'" [value]="category">{{category}}</mat-option>
</ng-container>
</mat-select>
<mat-error *ngIf="category.invalid && !''">{{getErrorCategory()}}</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="picker1" placeholder="Upload Date:" [formControl]="uploaded">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>
<div class="buttons">
<ng-container *ngIf="data; then update; else add"></ng-container>
<ng-template #add>
<button mat-raised-button color="primary" (click)="onAdd()" [disabled]="title.invalid || category.invalid">Add
item</button>
</ng-template>
<ng-template #update>
<button mat-raised-button color="primary" (click)="onUpdate()"
[disabled]="title.invalid || category.invalid">Update
item</button>
</ng-template>
<button mat-raised-button color="primary" (click)="onCancel()">Cancel</button>
</div>
</section>
</div>
模板中最重要的是按钮,因此当父组件有输入数据时显示按钮更新或没有任何数据时添加按钮:
<ng-container *ngIf="data; then update; else add"></ng-container>
<ng-template #add>
<button mat-raised-button color="primary" (click)="onAdd()" [disabled]="title.invalid || category.invalid">Add
item</button>
</ng-template>
<ng-template #update>
<button mat-raised-button color="primary" (click)="onUpdate()"
[disabled]="title.invalid || category.invalid">Update
item</button>
</ng-template>
form.component.ts 使用@angular/material 库以防弹出对话框:
categories = MediaOptions.CATEGORIES
uploaded = new FormControl(new Date(), [Validators.required])
title = new FormControl('', [Validators.minLength(5), Validators.required])
category = new FormControl('', [Validators.required])
constructor(private store: Store,
private snackBar: SnackService,
public dialogRef: MatDialogRef<AddItemComponent>,
@Inject(MAT_DIALOG_DATA) public data?: any) {}
ngOnInit(): void {
if(this.data) {
this.title.setValue(this.data.title)
this.category.setValue(this.data.category)
this.uploaded.setValue(this.data.uploaded)
} else {
this.uploaded.setValue(new Date())
}
}
带有功能打开对话框的父组件:
constructor(private store: Store, private snackBar: SnackService, private dialog: MatDialog) { }
async ngOnInit() {
this.store.dispatch(new GetMedia())
}
editItem(payload: any) {
this.dialog.open(AddItemComponent,
{ width: '500px', data: payload });
}
addItem() {
this.dialog.open(AddItemComponent,
{ width: '500px' });
}