How do I filter an array with TypeScript in Angular 2?

前端 未结 3 1612
耶瑟儿~
耶瑟儿~ 2020-12-04 11:56

ng-2 parent-child data inheritance has been a difficulty for me.

What seems that could be a fine working practical solution is filtering my total array of data to a

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 12:23

    You need to put your code into ngOnInit and use the this keyword:

    ngOnInit() {
      this.booksByStoreID = this.books.filter(
              book => book.store_id === this.store.id);
    }
    

    You need ngOnInit because the input store wouldn't be set into the constructor:

    ngOnInit is called right after the directive's data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.

    (https://angular.io/docs/ts/latest/api/core/index/OnInit-interface.html)

    In your code, the books filtering is directly defined into the class content...

提交回复
热议问题