2016-12-02 28 views
0

と、未定義の私が作成し、ビューをロード私はこの例外を取得し、アレイ用のカスタムフィルタパイプを追加したので:プロパティを読み取ることができません「0」カスタムフィルタパイプ

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

これは何かのTODOを持っている必要があります私はデータのロード方法を推測してください。

パイプデータロードを追加する前に作業しました!

パイプにブレークポイントを置くことはできますが、エラーのために内部にヒットすることはありません。

誰も私の最初のパイプで何が間違っているのか考えていますか? '角度/コア@' から

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

これはハムです。それは変換後の行であるため、配列からパラメータを読み込もうとします。 –

答えて

1

Pipe#transformは、オプションの最上位引数を取ります.1つは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

ありがとう、それは今働く、私は古くからのブログを読むようです... argsとargs?問題! – Pascal

関連する問題