2016-12-02 73 views
0

既然我已經創建並添加自定義過濾器管的一個數組我得到這個例外,當視圖加載:不能與自定義過濾器管讀取屬性「0」的未定義

TesttypesListComponent - inline template:35:14 caused by: Cannot read property '0' of undefined 

這必須有一些待辦事項我猜測我如何加載數據。

在我添加管道數據加載工作!

我可以在管道中放置一個斷點,但由於錯誤它永遠不會打到裏面。

任何人有一個想法,我做錯了我的第一個管道?

PIPE

進口{管,PipeTransform}從 '@角/芯';

@Pipe({ 
    name: 'TestTypePipe' 
}) 
export class TestTypePipe implements PipeTransform { 
    transform(testTypes, args?) { 
     let [currentSubject, currentSchoolclass] = args; 
     if (!currentSubject || !currentSchoolclass) { 
      return []; 
     } 
     else { 
      return testTypes.filter(s => { 
       return s.subjectId == currentSubject.subjectId && s.schoolclassId == currentSchoolclass.schoolclassId; 
      }); 
     } 
    } 
} 

HTML

<tbody> 
      <tr *ngFor="let t of testTypes | TestTypePipe:currentSubject:currentSchoolclass; let i = index"> 
      <template [ngTemplateOutlet]="getTemplate(t)" [ngOutletContext]="{ $implicit: t, index: i }"></template> 
      </tr> 
     </tbody> 

COMPONENT

currentSubject: Subject; 
    currentSchoolclass: Schoolclass; 

     ngOnInit() { 
this.route.params.subscribe(params => { this.schoolyearId = parseInt(params['id']); }); 

     this.testService.getTestTypes(this.schoolyearId).subscribe(data => { 
      this.schoolclasses = data.schoolclasses.map(m => new Schoolclass(m)); 
      this.subjects = data.subjects.map(m => new Subject(m)); 

      for (let t of data.testTypes) { 
      t.viewMode = ViewMode.Displaying; 
      let testType = new AssignedTestType(t); 
      this.testTypes.push(testType) 
      } 
     } 

); }

+0

如果刪除參數':currentSubject:currentSchoolclass',是否會調用管道? –

+0

然後我得到這個錯誤:無法在http:// localhost:4200/main.bundle上讀取TestTypePipe.transform(http:// localhost:4200/main.bundle.js:72680:34)處未定義的屬性'0' .js:20150:22 at =>似乎我會取消和console.log明天的一些東西,找到未定義的... – Pascal

+0

嗡嗡聲,正確的,這是因爲let行內部轉換,它試圖從數組中讀取參數 –

回答

1

Pipe#transform需要可選頂級參數,而不是單個args: []

import { Pipe, PipeTransform } from '@angular/core'; 

@Pipe({ 
    name: 'TestTypePipe' 
}) 
export class TestTypePipe implements PipeTransform { 
    transform(testTypes, ...args) { 
     let [currentSubject, currentSchoolclass] = args; 
     if (!currentSubject || !currentSchoolclass) { 
      return []; 
     } 
     else { 
      return testTypes.filter(s => { 
       return s.subjectId == currentSubject.subjectId && s.schoolclassId == currentSchoolclass.schoolclassId; 
      }); 
     } 
    } 
} 
+0

感謝它現在的作品,似乎我閱讀過時的博客,由於...參數和參數?問題! – Pascal

相關問題