简体   繁体   English

无法分配给“fromDate”,因为它是只读属性。ts

[英]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">...

类型错误:无法分配给 object 的只读属性 'backgroundColor' '#<object> '<div id="text_translate"><p> 我正在使用 React 创建一个组件,该组件将根据其属性呈现不同的背景。 但是,我不知道如何在不折叠 React 框架的情况下更改其属性。 这是我的代码:</p><pre> const inactiveStyle = { backgroundColor: "", color: "white", }; const activeStyle = { color: "black" }; export default class DrumPad extends Component { constructor(props) { super(props); this.state = { style: inactiveStyle, }; render(){ inactiveStyle.backgroundColor = this.props.backgroundColor return (&lt;div style={this.state.style}&gt;&lt;/div&gt;)</pre><p> 当我尝试它时,这是我得到的错误:</p><p> TypeError:无法分配给 object '#' 的只读属性 'backgroundColor'</p><p> 我已尝试按照<a href="https://stackoverflow.com/questions/52143027/reactjs-typeerror-cannot-assign-to-read-only-property-transform-of-object" rel="nofollow noreferrer">此处</a>的建议进行操作,但它不起作用,因为我的原始代码已经是一个常量,而不是 state 本身。 请帮忙。</p></div></object> - Type Error: Cannot assign to read only property 'backgroundColor' of object '#<Object>'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSX 元素类不支持属性,因为它没有“道具”属性。ts(2607) - JSX element class does not support attributes because it does not have a 'props' property.ts(2607) 无法分配给字符串“”的只读属性“0” - Cannot assign to read only property '0' of string ' ' TypeError:无法分配为只读属性“选中”复选框 - TypeError: Cannot assign to read only property 'checked' checkbox 未捕获的类型错误:无法分配给 object 的只读属性 'tagName' '#<htmlimageelement> '</htmlimageelement> - Uncaught TypeError: Cannot assign to read only property 'tagName' of object '#<HTMLImageElement>' 类型错误:无法分配给 object 的只读属性 'backgroundColor' '#<object> '<div id="text_translate"><p> 我正在使用 React 创建一个组件,该组件将根据其属性呈现不同的背景。 但是,我不知道如何在不折叠 React 框架的情况下更改其属性。 这是我的代码:</p><pre> const inactiveStyle = { backgroundColor: "", color: "white", }; const activeStyle = { color: "black" }; export default class DrumPad extends Component { constructor(props) { super(props); this.state = { style: inactiveStyle, }; render(){ inactiveStyle.backgroundColor = this.props.backgroundColor return (&lt;div style={this.state.style}&gt;&lt;/div&gt;)</pre><p> 当我尝试它时,这是我得到的错误:</p><p> TypeError:无法分配给 object '#' 的只读属性 'backgroundColor'</p><p> 我已尝试按照<a href="https://stackoverflow.com/questions/52143027/reactjs-typeerror-cannot-assign-to-read-only-property-transform-of-object" rel="nofollow noreferrer">此处</a>的建议进行操作,但它不起作用,因为我的原始代码已经是一个常量,而不是 state 本身。 请帮忙。</p></div></object> - Type Error: Cannot assign to read only property 'backgroundColor' of object '#<Object>' 如何修复无法分配给 Angular 中的只读属性 - how to fix cannot assign to read only property in Angular 如何通过单击按钮更改表单输入的只读属性 - How to change read-only property of a form input with the click of button 未捕获的类型错误:无法分配给 object '[object Array]' js 的只读属性 '0' - Uncaught TypeError: Cannot assign to read only property '0' of object '[object Array]' js 制作视图的最佳方式,angular ts 中 routeroutlet 部分的只读 - The best way to make the view, a read-only of routeroutlet 's sections in angular ts 我该如何解决在React中无法分配为只读对象的属性“ exports”? - How do I solve Cannot assign to read only property 'exports' of object in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM