<mat-button-toggle-group appearance="legacy"    [(ngModel)]="selectedClockType">
        <mat-button-toggle *ngFor="let item of items" [value]="item" color="primary">
           {{ item.label }}
        </mat-button-toggle>  
    </mat-button-toggle-group>
       
     @for (item of items; track:item.id) {   
          <ng-container *ngComponentOutlet="item .component"></ng-container> 
        }

     <ng-template #actionItesm let-data="data">{{data}} </ng-template>

Component-a.html

    <ng-content></ng-content>

Component-b.html

<ng-content></ng-content>

正在try 使用以下代码传递ng模板

 <ng-container *ngComponentOutlet="item .component;content: actionItesm ></ng-container> 

并使用内部正在使用的所有组件Items

推荐答案

您需要使用ViewContainerRef中的createEmbeddedView,其中我们输入模板作为第一个参数,然后是包含模板上下文的第二个参数.我创建了一个新属性templateRef,其中包含创建的模板ref struct .然后最后我们将该输入传递给属性content(满分*ngComponentOutlet)

NgComponentOutlet中提到的例子是理解步骤和过程的一个很好的例子.

完整代码:完整代码:

父 TS :

import { CommonModule } from '@angular/common';
import {
  Component,
  ViewChild,
  ViewContainerRef,
  TemplateRef,
  Injector,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { AComponent } from 'src/app/a/a.component';
import { BComponent } from 'src/app/b/b.component';

/**
 * @title Basic button-toggles
 */
@Component({
  selector: 'button-toggle-overview-example',
  templateUrl: 'button-toggle-overview-example.html',
  standalone: true,
  imports: [
    MatButtonToggleModule,
    CommonModule,
    AComponent,
    BComponent,
    FormsModule,
  ],
})
export class ButtonToggleOverviewExample {
  @ViewChild('actionItesm', { static: true }) actionItesm!: TemplateRef<any>;
  selectedClockType: any;
  items: Array<any> = [
    { id: 1, label: 'a', component: AComponent },
    { id: 2, label: 'b', component: BComponent },
  ];
  myContent?: any[][];
  myInjector: Injector;

  constructor(injector: Injector, private vcr: ViewContainerRef) {
    this.myInjector = injector;
  }

  ngOnInit() {
    // Create the projectable content from the templates
    if (this.items?.length) {
      this.items.forEach((item: any) => {
        item.templateRef = [
          this.vcr.createEmbeddedView(this.actionItesm, { item: item })
            .rootNodes,
        ];
      });
    }
  }
}

父 HTML :

<mat-button-toggle-group appearance="legacy" [(ngModel)]="selectedClockType">
  <mat-button-toggle *ngFor="let item of items" [value]="item" color="primary">
    {{ item.label }}
  </mat-button-toggle>
</mat-button-toggle-group>
<br />
<br />
<br />
@for (item of items; track item.id;) { {{item.label}}
<ng-container
  *ngComponentOutlet="item.component;
  content: item.templateRef;"
></ng-container>
}
<ng-template #actionItesm let-item="item">{{item.label}} asdf</ng-template>

children TS :

import { Component } from '@angular/core';

@Component({
  selector: 'app-b',
  standalone: true,
  imports: [],
  templateUrl: './b.component.html',
  styleUrl: './b.component.scss'
})
export class BComponent {

}

子 HTML :

<br />
<hr />
B COMPONENT
<ng-content></ng-content>
<hr />
<br />

Stackblitz Demo

Javascript相关问答推荐

Vue v模型检测父参考更改

v-textfield的规则找不到数据中声明的元素

在JavaScript中打开和关闭弹出窗口

JavaScript:循环访问不断变化的数组中的元素

在具有焦点和上下文的d3多线图表中,如何将上下文的刷新限制在特定日期?

在JavaScript中检索一些文本

如何在JavaScript中在文本内容中添加新行

使用复选框在d3.js多折线图中添加或删除线条

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

React Code不在装载上渲染数据,但在渲染上工作

如何修复(或忽略)HTML模板中的TypeScript错误?'

手机上的渲染错误文本必须在文本组件中渲染,但在浏览器上没有问题<><>

单个HTML中的多个HTML文件

当输入字段无效时,我的应用程序不会返回错误

Jest toHaveBeenNthCalledWith返回当前设置的变量值,而不是调用时的值

在画布中调整边上反弹框的大小失败

在Vercel中部署Next.js项目时获取`ReferenceError:未定义文档`

在查看网页时,如何使HTML中的按钮工作方式类似于鼠标上的滚轮或箭头键?

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件