填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

OnChanges

一個生命週期鉤子,當指令的任何一個可繫結屬性發生變化時呼叫。 定義一個 ngOnChanges() 方法來處理這些變更。

A lifecycle hook that is called when any data-bound property of a directive changes. Define an ngOnChanges() method to handle the changes.

      
      interface OnChanges {
  ngOnChanges(changes: SimpleChanges): void
}
    

參見

方法

如果至少發生了一次變更,則該回調方法會在預設的變更檢測器檢查完可繫結屬性之後、檢視子節點和內容子節點檢查完之前呼叫。

A callback method that is invoked immediately after the default change detector has checked data-bound properties if at least one has changed, and before the view and content children are checked.

ngOnChanges(changes: SimpleChanges): void
      
      ngOnChanges(changes: SimpleChanges): void
    
引數
changes SimpleChanges

那些發生了變化的屬性。

The changed properties.

返回值

void

使用說明

下列程式碼片段展示了元件要如何實現本介面來定義一個輸入屬性的變更處理器。

The following snippet shows how a component can implement this interface to define an on-changes handler for an input property.

@Component({selector: 'my-cmp', template: `...`}) class MyComponent implements OnChanges { // TODO(issue/24571): remove '!'. @Input() prop!: number; ngOnChanges(changes: SimpleChanges) { // changes.prop contains the old and the new value... } }
      
      @Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnChanges {
  // TODO(issue/24571): remove '!'.
  @Input() prop!: number;

  ngOnChanges(changes: SimpleChanges) {
    // changes.prop contains the old and the new value...
  }
}