[英]Cannot assign to 'fromDate' because it is a read-only property.ts
I'm using angular 12 .我正在使用angular 12 。 I have 2 DatePickers and they are inside form .Each DatePicker has small X button.我有 2 个DatePickers ,它们在form内。每个 DatePicker 都有小 X 按钮。 I want that when i press on it,it clears input in DatePicker.But when i try to clear it this way: this.fromDate=null this error comes: Cannot assign to 'fromDate' because it is a read-only property.ts.我希望当我按下它时,它会清除 DatePicker 中的输入。但是当我尝试以这种方式清除它时: this.fromDate=null出现此错误:无法分配给“fromDate”,因为它是只读属性.ts . I tried function this.fromDate.setValue() but it says that function doesn't exist.我试过 function this.fromDate.setValue() 但它说 function 不存在。 Anything else I found that mentions this error they suggest what i mentioned before or something that i can't do since this is in form.我发现的任何其他提到此错误的内容都暗示了我之前提到的内容或我不能做的事情,因为这是形式。
Also tried this.filterForm.reset() but it resets both Datepickers since they are in group.还尝试了 this.filterForm.reset()但它重置了两个日期选择器,因为它们在组中。
Here is my code:这是我的代码:
this is my html code这是我的 html 代码
<div class="title-group" >
<h1 class="mat-h1" >Popis vožnji</h1>
</div>
<div >
<mat-divider fxFlex="1 0"></mat-divider>
</div >
<div>
<mat-form-field appearance="standard">
<mat-label>Filter</mat-label>
<input matInput (keyup)="applyFilter($event)" placeholder="Ex. Lift0" #input>
</mat-form-field>
</div>
<div >
<form [formGroup]="filterForm">
<mat-form-field class="example-form-field1">
<input matInput
[matDatepicker]="fromDate"
placeholder="From Date"
formControlName="fromDate">
<button *ngIf="fromDate" matSuffix mat-icon-button aria-label="Clear" (click)="clearFromDate()">
<mat-icon matDatepickerToggleIcon >close</mat-icon>
</button>
<mat-datepicker-toggle matSuffix [for]="fromDate"></mat-datepicker-toggle>
<mat-datepicker #fromDate></mat-datepicker>
</mat-form-field>
<mat-form-field class="example-form-field1">
<input matInput
[matDatepicker]="toDate"
placeholder="To Date"
formControlName="toDate">
<button *ngIf="toDate" matSuffix mat-icon-button aria-label="Clear" (click)="clearToDate($event)">
<mat-icon matDatepickerToggleIcon >close</mat-icon>
</button>
<mat-datepicker-toggle matSuffix [for]="toDate"></mat-datepicker-toggle>
<mat-datepicker #toDate></mat-datepicker>
</mat-form-field>
<button mat-button color="primary" (click)="applyFilterDate()">Pretraži</button>
</form>
</div>
</div>
this is my ts code这是我的 ts 代码
export class IspisPutovanjaComponent implements OnInit {
id :string|null;
LiftData: any = [];
dataSource!: MatTableDataSource<Putovanje>;
@ViewChild(MatPaginator)
paginator!: MatPaginator;
@ViewChild(MatSort)
sort!: MatSort;
displayedColumns: any[] = [
'n_k',
'v_k',
'p_k',
'z_k',
'start_time',
'end_time',
'count_p'
];
//
pipe: DatePipe;
filterForm = new FormGroup({
fromDate: new FormControl(),
toDate: new FormControl(),
});
get fromDate() { return this.filterForm.get('fromDate')!.value; }
get toDate() { return this.filterForm.get('toDate')!.value; }
constructor(private travel_service: PutovanjeService,private actRoute: ActivatedRoute) {
this.id = this.actRoute.snapshot.paramMap.get('id');
this.dataSource= new MatTableDataSource();
this.pipe = new DatePipe('en');
}
ngOnInit(): void {
this.LiftData = [];
this.retrieveLifts();
}
applyFilter(event: Event) {
const filterValue = (event.target as HTMLInputElement).value;
this.dataSource.filter = filterValue.trim().toLowerCase();
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
//
applyFilterDate() {
this.dataSource.data=this.LiftData.filter( (data: { start_time: string | number | Date; end_time: string | number | Date; })=>{
if (this.fromDate && this.toDate) {
return this.pipe.transform(data.start_time,'shortDate')! >= this.pipe.transform(this.fromDate,'shortDate')! && this.pipe.transform(data.end_time,'shortDate')! <= this.pipe.transform(this.toDate,'shortDate')!;
}
return true;
}
);
}
clearFromDate()
{
this.fromDate=null;
}
clearToDate(event: { stopPropagation: () => void; }){
event.stopPropagation();
this.dataSource.data=this.LiftData;
}
}
you has a template reference variable the: "#fromDate" in your你有一个模板引用变量 the: "#fromDate" in your
<mat-datepicker #fromDate ..>
With the same name than you getter和你一样的名字
get fromDate(){..}
Just change the first from some like <mat-datepicker #fromDateID..>
只需将第一个更改为<mat-datepicker #fromDateID..>
See that, in this case the toogle button becomes like <mat-datepicker-toggle [for]="fromDateID">...
看,在这种情况下,toogle 按钮变得像<mat-datepicker-toggle [for]="fromDateID">...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.