[英]behaviour of ngIf and property binding in angular
<myview [myVariable]=myVariable *ngIf="myVariable">
</myview>
我定義了上面的組件模板,它將 myVariable 屬性綁定到 myview 組件的 myVariable 屬性。 如果 myVariable 發生變化,myview 組件是否應該重新加載(構造函數或 ngOnInit)。 它不在我的環境中,因此很困惑,如果這是預期的行為。 該組件僅被實例化一次,我希望它在對父組件中的 myVariable 進行任何更改后刷新其值。
您可以在ngOnChanges
獲得該事件,這是示例。
您需要在myview
組件中編寫ngOnChanges
import { Component, OnInit, Input, AfterViewInit, OnChanges, SimpleChanges, ViewChild, Output, EventEmitter } from '@angular/core';
@Input() myVariable: any;
class myViewComponent implements OnChanges, OnInit {
ngOnChanges(changes: SimpleChanges): void {
console.log(changes.myVariable); // here you will get the updated value of myVariable
}
}
你的父組件 html
<myview [myVariable]="myVariable" *ngIf="myVariable">
</myview>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.