How to pass template data to related components instead of child or parent components without creating new components?
My project.html
<div *ngFor="let item of items"> <div *ngFor="let subItem of subItems"> <div *ngIf="!item.value.length"> <div class="cloth {{nextAvailableSubItem}}"></div> </div> </div> </div>
my-item.component.ts
this.items = {one: ['redShirt'], two: [], three: [] four: ['whiteShirt', 'blackShirt']} this.subItems = ['redShirt', 'blueShirt', 'whiteShirt', 'blackShirt']; filterItems(item, subItem){ return nextAvailableSubItem //在redShirt之后应该返回whiteShirt以在模板中使用该值 }
A common approach is to use Observable to share data through an intermediate service
As follows
Don't forget to unsubscribe when the consumer component is destroyed, otherwise it will cause a memory leak.