Bind a service property to a component property with proper change tracking

前端 未结 3 862
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 12:19

Consider the utterly simple Angular 2 service:

import { Injectable } from \'@angular/core\';
import {Category} from \"../models/Category.model\";

@Injectabl         


        
3条回答
  •  遥遥无期
    2021-01-01 12:46

    You may try to substiture ngOnInit() with ngDoCheck(). I am not sure (actually I doubt) this is the right thing to do, in any case you can try.

    This method is run at every change detection cycle (instead of the standard Angular algorithm I guess, and here is the potential issue) and therefore you shoud have the category property of MySelectorComponent up to date with the changes in the service.

    Again need to be carefull of side effects (which are not clear to me).

提交回复
热议问题