简体   繁体   English

何时调用ngOnInit方法?

[英]When is ngOnInit method called?

I have the following template 我有以下模板

<div *ngIf="hotels$ | async as hotels; else loadGif ">
   <div *ngFor="let hotel of hotels | hotelsFilter: _filteredType; first as f; index as i " 
    appInit [hotel]="hotel " [first]="f ">
    <app-card appHighlight [hotel]=hotel></app-card>
   </div>
</div>

And appInit directive is written to idenify the fist element of hotels list after the list is filtered. 并在过滤列表后编写appInit指令以识别酒店列表的第一元素。 This is ngInit method of appInit directive: 这是appInit指令的ngInit方法:

  constructor(
        private __hotelsService: HotelsService
      ) { }

      @Input()
      public hotel: Hotel;
      @Input()
      public first: boolean;

      public ngOnInit(): void {
        if (this.first) {
          this.__hotelsService.selectedHotel$.next(this.hotel);
          console.log('first init ' + this.hotel.name);
        }else{
          console.log('not first init ' + this.hotel.name);
        }

      }

The problem is that ngOnInit method of the directive is called not each time when the list of hotels is filtered and re-rendered. 问题是该指令的ngOnInit方法不会在每次筛选并重新呈现酒店列表时都调用。 The question is why and when ngOnInit is really should be called? 问题是,为什么以及何时真正应该调用ngOnInit?

Every time change detection runs and updates inputs that have a value bound to them ngOnChanges is called. 每次运行变更检测并更新具有绑定到它们的值的输入时,就会调用ngOnChanges
ngOnInit is called the first time after ngOnChanges is called. 在调用ngOnInit之后第一次调用ngOnChanges

For more details see https://angular.io/guide/lifecycle-hooks 有关更多详细信息,请参见https://angular.io/guide/lifecycle-hooks

When *ngFor renders the elements, it recognizes when values are the same and reuses rendered items. *ngFor渲染元素时,它将识别何时值相同,并重新使用渲染的项目。 This is why ngOnInit is not called after the filter changes for items that were also rendered before the filter changed. 这就是为什么ngOnInit在过滤器更改之前也已呈现的项目,在过滤器更改之后不调用ngOnInit原因。

You can instead use ngOnChanges which will be called every time an input is updated, or you can make the inputs getters 您可以改用ngOnChanges ,每次更新输入时都会调用ngOnChanges ,也可以使输入获取器

  _first: boolean,
  @Input()
  public set first(value: boolean) {
    this._first = value;
    if (this.first) {
      this.__hotelsService.selectedHotel$.next(this.hotel);
      console.log('first init ' + this.hotel.name);
    }else{
      console.log('not first init ' + this.hotel.name);
    }
  }

  public get first() : boolean {
    return this._first;
  }

ngOnInit will only be called once when the component is initialized. 初始化组件时,只会调用一次ngOnInit As per your need, you should use ngOnChanges which will be called when input properties get changed. 根据您的需要,您应该使用ngOnChanges ,当输入属性被更改时将被调用。

For more info on lifecycle hooks: https://angular.io/guide/lifecycle-hooks 有关生命周期挂钩的更多信息: https : //angular.io/guide/lifecycle-hooks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM