Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

angular - 在异步管道重新订阅上重构的角子组件(Angular child component reconstructed on async pipe resubscription)

I am using 'ngFor' directive to initialize a list of child components:

(我正在使用ngFor指令初始化子组件列表:)

Parent component:

(父组件:)

 <div *ngFor="let item of serviceGetter.items | async; let i = index">
     <child-component [item]="item" [currentIndex]="i"></child-component>
</div>

Service:

(服务:)

@Injectable()
export class TermsService {

   private dataStore: {
     data: any[];
   } = { data: [] };

   get items() {
     return this._data$.asObservable();
   }

   //data are populated inside this method
   someMethod() {
      this.dataStore.data= result;
      this._data$.next(Object.assign({}, this.dataStore).data);
   }

   updateSpecificItem() {
       ....          

      this.dataStore.data[index] = {
         ...somedata,
      };
   }
}

'Child component' change detection is set to 'OnPush'.

(“子组件”更改检测设置为“ OnPush”。)

The problem is when 'updateSpecificItem()' function is called child component at 'index' position is reinitialized(constructor call).

(问题是当“ updateSpecificItem()”函数在“索引”位置的子组件被重新初始化(构造函数调用)时。)

Is there a solution to avoid this or is this the accepted behavior?

(有解决方案可以避免这种情况吗?还是这是公认的行为?)

  ask by tsiro translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...