将组件中的 ng-template 传递到另一个组件中的 ng-select
Pass ng-template From A Component To ng-select Inside Another Component
我正在尝试将 ng-template 从我的主要组件传递到另一个组件内的 ng-select。我该怎么做?
已经尝试使用 ng-content 或模板参考,但仍然无法正常工作。
我想要实现的与 ng-select 自定义模板示例完全相同,只是 ng-template 是由另一个组件提供的。所以相反:
<ng-select [items]="cities" [(ngModel)]="selectedCity" bindLabel="name" bindValue="name">
<ng-template ng-label-tmp let-item="item">
<img height="15" width="15" [src]="item.avatar"/>
{{item.name}}
</ng-template>
</ng-select>
我正在努力实现与此类似的目标:
select-custom.component.html
<ng-select [items]="cities" [(ngModel)]="selectedCity" bindLabel="name" bindValue="name">
<ng-content></ng-content> --> this is not working, templateref also not working
</ng-select>
app.component.html
<app-select-custom>
<ng-template ng-label-tmp let-item="item">
<img height="15" width="15" [src]="item.avatar"/>
{{item.name}}
</ng-template>
</app-select-custom>
回答我自己的问题。感谢@yurzui:
stackblitz.com/edit/ng-select-43wmxu?file=app/app.component.ts
自定义中的 ContentChild-select.component.ts
@Component({
selector: 'app-custom-select',
templateUrl: './custom-select.component.html',
styleUrls: ['./custom-select.component.css']
})
export class CustomSelectComponent {
@ContentChild(TemplateRef) template: TemplateRef<any>
cities = [
{id: 1, name: 'Vilnius', avatar: 'https://avatars.io/platform/userId'},
{id: 2, name: 'Kaunas', avatar: 'https://avatars.io/platform/userId'},
{id: 3, name: 'Pavilnys', disabled: true, avatar: 'https://avatars.io/platform/userId'},
{id: 4, name: 'Pabradė', avatar: 'https://avatars.io/platform/userId'},
{id: 5, name: 'Klaipėda', avatar: 'https://avatars.io/platform/userId'}
];
}
自定义-select.component.html
<ng-select [items]="cities" [(ngModel)]="selectedCity" bindLabel="name" bindValue="name">
<ng-template ng-label-tmp let-item="item">
<ng-template [ngTemplateOutlet]="template" [ngTemplateOutletContext]="{item: item}"></ng-template>
</ng-template>
</ng-select>
app.component.html
<app-custom-select>
<ng-template ng-label-tmp let-item="item">
<img height="15" width="15" [src]="item.avatar"/>
{{item.id}}
</ng-template>
</app-custom-select>
我正在尝试将 ng-template 从我的主要组件传递到另一个组件内的 ng-select。我该怎么做?
已经尝试使用 ng-content 或模板参考,但仍然无法正常工作。
我想要实现的与 ng-select 自定义模板示例完全相同,只是 ng-template 是由另一个组件提供的。所以相反:
<ng-select [items]="cities" [(ngModel)]="selectedCity" bindLabel="name" bindValue="name">
<ng-template ng-label-tmp let-item="item">
<img height="15" width="15" [src]="item.avatar"/>
{{item.name}}
</ng-template>
</ng-select>
我正在努力实现与此类似的目标:
select-custom.component.html
<ng-select [items]="cities" [(ngModel)]="selectedCity" bindLabel="name" bindValue="name">
<ng-content></ng-content> --> this is not working, templateref also not working
</ng-select>
app.component.html
<app-select-custom>
<ng-template ng-label-tmp let-item="item">
<img height="15" width="15" [src]="item.avatar"/>
{{item.name}}
</ng-template>
</app-select-custom>
回答我自己的问题。感谢@yurzui:
stackblitz.com/edit/ng-select-43wmxu?file=app/app.component.ts
自定义中的 ContentChild-select.component.ts
@Component({
selector: 'app-custom-select',
templateUrl: './custom-select.component.html',
styleUrls: ['./custom-select.component.css']
})
export class CustomSelectComponent {
@ContentChild(TemplateRef) template: TemplateRef<any>
cities = [
{id: 1, name: 'Vilnius', avatar: 'https://avatars.io/platform/userId'},
{id: 2, name: 'Kaunas', avatar: 'https://avatars.io/platform/userId'},
{id: 3, name: 'Pavilnys', disabled: true, avatar: 'https://avatars.io/platform/userId'},
{id: 4, name: 'Pabradė', avatar: 'https://avatars.io/platform/userId'},
{id: 5, name: 'Klaipėda', avatar: 'https://avatars.io/platform/userId'}
];
}
自定义-select.component.html
<ng-select [items]="cities" [(ngModel)]="selectedCity" bindLabel="name" bindValue="name">
<ng-template ng-label-tmp let-item="item">
<ng-template [ngTemplateOutlet]="template" [ngTemplateOutletContext]="{item: item}"></ng-template>
</ng-template>
</ng-select>
app.component.html
<app-custom-select>
<ng-template ng-label-tmp let-item="item">
<img height="15" width="15" [src]="item.avatar"/>
{{item.id}}
</ng-template>
</app-custom-select>