【发布时间】:2018-01-09 05:02:57
【问题描述】:
你能告诉我如何在父页面中访问内部子页面的公共变量吗?即我需要访问这个变量offlineArticlesCount。
注意:以下所有 3 个组件都有自己的modules。
myPage.html - 父页面
<picks *ngFor="let pick of pickData" [data]="pick"></picks>
<p>{{instance.offlineArticlesCount}}</p> //How to do this?
myPage.ts
@ViewChild(DownloadOfflineArticleComponent) instance: DownloadOfflineArticleComponent;
picks.html
<download-offline-article [data]="data" (onDownloadOfflineArticle)="downloadOfflineArticle($event)"></download-offline-article>
download-offline-article.ts - 内部子组件
export class DownloadOfflineArticleComponent {
offlineArticlesCount: number = 0;
constructor(){}
downloadOfflineArticle() {
this.articleService.downloadOfflineArticle(this.data.id)
.subscribe((res: any) => {
this.onDownloadOfflineArticle.emit(true);
this.localCacheService.clearMyLibraryPageCacheGroup().then(() => {
this.getAllMyPurchasedOfflineArticles().subscribe((res: any) => {
this.offlineArticlesCount = res.count;//here is the place where it updates
},
error => { },
() => { });
});
},
error => { },
() => { });
}
}
【问题讨论】:
-
您是否已经尝试过像这样使用 ViewChild:
@ViewChild(DownloadOfflineArticleComponent) instance: DownloadOfflineArticleComponent;,然后在视图中使用{{ instance?. offlineArticlesCount}}?
标签: angular typescript ionic2 ionic3