我有两个组件。
父组件的.html代码如下:
<div class="space">
<ng-container *ngFor="let item of items">
<ng-container [ngTemplateOutlet]="itemTemplate" [ngTemplateOutletContext]="{item: item}">
</ng-container>
</ng-container>
</div>
<ng-template #itemTemplate let-item="item">
<div class="host">
<img class='card-img-top'
src={{item.image}}
>
<p> </p>
<h6 class="title">{{item.projectTitle}}</h6>
<hr>
<ng-container *ngIf="item.type=='inactive'">
<button id="inactive"
class="btn btn-sm btn-warning py-0 custom-button text-uppercase">{{item.type}}</button>
</ng-container>
</div>
</ng-template>从以下行开始的所有代码:<ng-template #itemTemplate let-item="item">
它有必要在子组件的.html中显示。
这是父组件的.ts
import { AfterViewInit, Component, ComponentFactoryResolver, ViewChild, ViewContainerRef, OnInit, TemplateRef, Input, ContentChild } from '@angular/core';
import listProjects from './projects.json';
import { ChildComponent } from './child.component';
@Component({
selector: 'parent-view',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss'],
})
export class ParentComponent {
public items: any = listProjects;
@ContentChild('itemTemplate', { read: TemplateRef }) itemTemplate: ViewContainerRef;如何在其他组件中显示<ng-template #itemTemplate let-item="item">?
发布于 2020-05-09 11:02:00
这个问题的解决方案是:
文件:projects-tile-view-components.ts
export class ProjectsTileViewComponent {
@ViewChild('tileViewItems', { static: true }) tileViewItemTemplate: TileViewComponent;
public items = listProjects;
public type = 'etb';
constructor() {}
addItems(e) {
if (this.type === 'osd') {
this.type = 'etb';
} else {
this.type = 'osd';
}
}
}文件projects-tile-view-components.html
<lib-tile-view [items]="items" #tileViewItems>
<ng-template #tileViewItem let-item>
<div [ngSwitch]="type">
<lib-osd-tile-view-item [item]="item" *ngSwitchCase="'osd'"></lib-osd-tile-view-item>
<lib-osd-tile-view-item [item]="item" *ngSwitchDefault></lib-osd-tile-view-item>
<lib-etb-tile-view-item [item]="item" *ngSwitchCase="'etb'"></lib-etb-tile-view-item>
</div>
</ng-template>
</lib-tile-view>文件view-item.ts (接口)
export interface ViewItem {
item: any;
}文件tile-view-components.html
<div>
<ng-container *ngFor="let item of items">
<ng-container *ngTemplateOutlet="tileViewItemTemplate; context: {$implicit: item}">
</ng-container>
</ng-container>
</div>文件tile-view-item.components.ts
@Component({
selector: 'lib-tile-view',
templateUrl: './tile-view.component.html',
styleUrls: ['./tile-view.component.scss'],
})
export class TileViewComponent {
@Input()
public items: any;
@ContentChild('tileViewItem', { static: true }) tileViewItemTemplate;
}文件osd-tile-view-components.ts
@Component({
selector: 'lib-osd-tile-view-item',
templateUrl: './osd-tile-view-item.component.html',
styleUrls: ['./osd-tile-view-item.component.scss'],
})
export class OsdTileViewItemComponent implements ViewItem {
@Input() item: any;
public projectView() {
alert('show project');
}
}文件osd-tile-view-item.components.html
<div class="host" fxFlex="0 1 calc(100% - 32px)" fxFlex.lt-md="0 1 calc(100% - 32px)" fxFlex.lt-sm="100%" (click) = 'projectView()'>
<img class='img-fluid img-thumbnail' src={{item?.image}} alt='logo nih'>
<p> </p>
<h6 class="title">{{item?.projectTitle}}</h6>
<p class="small"><span class="text-muted small text-uppercase">ZIA ID
Number:</span>{{item?.number}}</p>
</div>谢谢你的帮忙
发布于 2020-05-07 05:46:32
您需要在服务中共享此TemplateRef (如果您希望在应用程序中的每个组件中都有访问权限),然后在其他组件中可以读取此引用并对其执行某些操作-因为如果您只想将此引用传递给子组件,那么您可以在子组件中使用@Input(),但是如果它应该在所有组件中都可用,那么您需要创建服务。
服务:
@Injectable({
providedIn: 'root'
})
export class TemplateService {
someTemplate: TemplateRef<any>;
}componentA:
@Component({
selector: 'parent-view',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss'],
})
export class ParentComponent {
public items: any = listProjects;
@ViewChild('itemTemplate', { read: TemplateRef }) set itemTemplate(value) {
this.ts.someTemplate = value;
}
construtor(private ts: TemplateService) {}
}然后,在任何其他组件中,您都可以读取此模板引用
componentB:
@Component({
selector: 'component-b',
templateUrl: './component-b.component.html',
styleUrls: ['./component-b.component.scss'],
})
export class ComponentBComponent {
get itemTemplateReference() {
return this.ts.itemTemplate;
}
construtor(private ts: TemplateService) {}
}componentB的.html
<ng-container [ngTemplateOutlet]="itemTemplateReference"></ng-container>并且它应该呈现相同的<ng-template>
https://stackoverflow.com/questions/61645858
复制相似问题