代码之家  ›  专栏  ›  技术社区  ›  Sajjan Sarkar

无法读取未定义的属性“currentValue”

  •  0
  • Sajjan Sarkar  · 技术社区  · 6 年前

    我有一个外部组件和一些内部组件,外部组件将信息传递给内部组件。 [filterString] (这就是问题所在)。

    allSegmentsFilter 模型在外一个改变,我得到以下错误:

    TypeError: Cannot read property 'currentValue' of undefined
    

    <app-json-to-table-viewer [rawJSON]="x" [filterString]="allSegmentsFilter"></app-json-to-table-viewer>
    

    外部组件视图(为简洁而抽象) :

    <div class="col-lg-8">
       <div class="row">
          <mat-form-field>
             <input  matInput                     
             placeholder="Search all segments"
             [(ngModel)]="allSegmentsFilter" >
          </mat-form-field>
       </div>
       <div class="row" *ngFor="let x of payfilemessage;" >
          <div >          
             <app-json-to-table-viewer [rawJSON]="x" [filterString]="allSegmentsFilter"></app-json-to-table-viewer>
          </div>
          <br>
       </div>
    </div>
    

    外部组件代码

    export class OuterComponent implements OnInit {
    
    
      payfilemessage = [/**abstracted, assume this has some values */];
      allSegmentsFilter="";  
    
      constructor() { }
    
      applyPayFileMessageFilter(filterValue: string) {    
        console.log("TBD filter:"+filterValue);
      }  
    
      ngOnInit() {
    
      }
    
    }
    

    <mat-tab-group class="mat-elevation-z2">
        <mat-tab label="Keys as columns">
            <div>
                <mat-form-field>
                    <input  matInput 
                            (keyup)="applyFilter($event.target.value)" 
                            placeholder="Filter"
                            [(ngModel)]="filterString"
                            >
                 </mat-form-field>             
                 <table mat-table [dataSource]="KeysAsCols.Source" matSort>
                    <ng-container *ngFor="let key of KeysAsCols.Columns;" [matColumnDef]="key">
                    <th mat-header-cell *matHeaderCellDef mat-sort-header>{{key}} </th>
                    <td mat-cell *matCellDef="let element"> {{element[key]}} </td>
                    </ng-container>
                    <tr mat-header-row *matHeaderRowDef="KeysAsCols.Columns"></tr>
                    <tr mat-row *matRowDef="let row; columns: KeysAsCols.Columns;"></tr>
                 </table>
                 <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
            </div>
        </mat-tab>
        <mat-tab label="Keys as rows"> Keys as rows </mat-tab>
        <mat-tab label="Raw JSON "> Raw JSON </mat-tab>
      </mat-tab-group>
    

    内部组件代码:

    import { Component, OnInit, Input, SimpleChanges, ViewChild } from '@angular/core';
    import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
    
    @Component({
      selector: 'app-json-to-table-viewer',
      templateUrl: './json-to-table-viewer.component.html',
      styleUrls: ['./json-to-table-viewer.component.scss']
    })
    export class JsonToTableViewerComponent implements OnInit {
      @Input() rawJSON: any[];
      @Input() filterString:string="";
    
    
    
    
    
      @ViewChild(MatPaginator) paginator: MatPaginator;
      @ViewChild(MatSort) sort: MatSort;
    
    
      KeysAsCols = {
        Source: <MatTableDataSource<any>> null,
        Columns: <String[]> []
    
      };
    
      constructor() { }
    
      ngOnChanges(changes: SimpleChanges) {
    
        if (this.rawJSON) {
          if (changes.rawJSON.currentValue != changes.rawJSON.previousValue )
            {
              console.log('value changed!')
              this.KeysAsCols.Source = new MatTableDataSource<any>(this.rawJSON);          
              this.setAllKeys();
              this.KeysAsCols.Source.paginator=this.paginator;
              this.KeysAsCols.Source.sort=this.sort;  
            }
        }
    
      }
      setAllKeys(){
        this.KeysAsCols.Columns = this.rawJSON.length==0?[]:Object.keys(this.rawJSON[0]);    
      } 
      applyFilter(filterValue: string) {
        this.KeysAsCols.Source.filter = filterValue.trim().toLowerCase();
      }
    
      ngOnInit() {
      }
      ngAfterViewInit() {
    
    }
    
    }
    

    1 回复  |  直到 6 年前
        1
  •  2
  •   alokstar    6 年前

    您可以在onchanges函数中添加防御条件,如:

    if (this.rawJSON && changes.rawJSON) {
          if (changes.rawJSON.currentValue != changes.rawJSON.previousValue )
            {
              console.log('value changed!')
              this.KeysAsCols.Source = new MatTableDataSource<any>(this.rawJSON);          
              this.setAllKeys();
              this.KeysAsCols.Source.paginator=this.paginator;
              this.KeysAsCols.Source.sort=this.sort;  
            }
        }