使用 Angular 2,如何在不同的组件文件之间使用 ngFor 数据


使用

Angular 2,如何在不同的组件文件之间使用 ngFor 数据?

例如,我在playbar.component.ts文件中有这个:

...
<audio 
 gFor="let audio of sources"                  
 #mediaOne 
 [vgMedia]="mediaOne" 
 id="mediaOne" 
 crossorigin
 [src]="currentItem.src">
...

这个在tunes.component.ts模板我有(并导入了上面的文件(

...
<ul>
  <li *ngFor="let item of sources; let $index = index"
        (click)="onClickPlaylistItem(item, $index)"
        [class.selected]="item === currentItem">
        {{ item.src }}
  </li>
</ul>
...

对不起,我有点棱角分明的菜鸟。

要在组件之间共享数据,您可以使用服务。

更多关于它的信息这里

该服务将数据作为可观察量,2 个组件将订阅可观察量。

最新更新