0

私は私が(私のカスタムservice.tsの角度素材ダイアログ)MdDialogに送信コンポーネントサービスごとにcomponentInstanceプロパティのセットを変更するときのライフサイクルフックの必要性は何ですか?

dialogRef = this.dialog.open(component, config); 

を持っていると私はそのようcomponentInstanceにより、このコンポーネントのパブリックプロパティを変更します。

dialogRef.componentInstance.task = task; 

角度は私にエラーを示しています。オープンmodal.service.ts

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'dialog'. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?

完全なコードを

@Injectable() 
export class TasksPopupService { 

constructor(
    private dialog: MdDialog, 
    private router: Router, 
    private tasksService: TasksService 
) { } 

public open(component: any, id?: string) { 

if (id) { 
    this.tasksService.find(id) 
    .subscribe(task => { 
     this.bindDialog(component, task); 
    }); 
} else { 
    this.bindDialog(component, new Task()); 
    } 
} 

bindDialog(component, task: Task) { 
    let dialogRef; 
    let config = new MdDialogConfig(); 
    config.height = '80%'; 
    config.width = '70%'; 
    dialogRef = this.dialog.open(component, config); 
    dialogRef.componentInstance.task = task; 
    dialogRef.afterClosed().subscribe(res => { 
    this.router.navigate([{ outlets: { popup: null } }], { replaceUrl: true }); 
    }); 
    return dialogRef; 
    } 
} 

しかし、idが定義されていない場合は、エラーが(ELSEブロックで)のみ発生した私はそれが理由のだと思います(非同期)、およびブロックELSEが非同期ではありません観測返しthis.tasksService.find。確信はないけど。

+0

これはエラーをよりよく理解するのに役立ちます[「ExpressionChangedAfterItHasBeenCheckedError'エラーについて知っておくべきことすべて」(https://blog.angularindepth.com/everything-you-need-to-know-ab--ex-pressionchangedafterithbithcheckederithasbeencheckederror -error-e3fd9ce7dbb4) –

+0

ええ、私はそれを見つけました。それをありがとう。私はミディアムであなたに続いた! –

+0

それは素晴らしいです、あなたは大歓迎です –

答えて

0

角材のMdContainerでエラーが発生したため、多少の混乱があります。 サーバからデータを取得した場合、時間がかかることがありますが、新しいオブジェクトを渡すと速く発生し、変更の検出は終了しません。 また、それは親子コンポーネントではなく、ライフサイクルフックが期待通りに機能しない可能性があります。
解決策が見つかりましたが、正しくはありません。ただ速い解決策。

if (id) { 
    this.tasksService.find(id) 
    .subscribe(task => { 

     this.bindDialog(component, task); 
    }); 
} else { 
    Observable.of(new Task()).delay(300).subscribe(task => { 
    this.bindDialog(component, task); 
    }); 
} 

変更検出に遅延を使用しており、エラーはスローされません。

関連する問題