简体   繁体   English

根据在 component.ts 中作为 TemplateRef 的 ng-template 列表动态填充 ng-container

[英]Dynamically populate an ng-container based on a list of ng-template that are TemplateRef in component.ts

To further explain, I want to populate an ng-container based on a list of ng-templates.为了进一步解释,我想根据 ng 模板列表填充一个 ng 容器。 Each ng-template is added one after the other.每个 ng-template 一个接一个地添加。 In other words ng-template contains two buttons previous and next.换句话说,ng-template 包含两个按钮 previous 和 next。 previous removes current ng-template and returns the previous template. previous 删除当前的 ng-template 并返回以前的模板。 Next button removes the current ng-template and adds the next ng-template. Next 按钮删除当前的 ng-template 并添加下一个 ng-template。

My issue is referencing each template using TemplateRef in component.ts.我的问题是在 component.ts 中使用 TemplateRef 引用每个模板。 I tried getting each template reference from the component.ts constructor but this doesn't work.我尝试从 component.ts 构造函数获取每个模板引用,但这不起作用。 My code below explains the approach I used.我下面的代码解释了我使用的方法。

home.component.html home.component.html

    <div class="template-container">
      <!-- Ng Container -->
      <ng-container *ngTemplateOutlet="activeTemplate">
      </ng-container>
    
        <!-- Select Service -->
        <ng-template #allServices>
           <!-- TODO -->
        </ng-template>
    
      <!-- Select Worker -->
      <ng-template #worker>
        <!-- TODO -->
      </ng-template>
    
      <!-- Display Calendar -->
      <ng-template #calendar>
        <!-- TODO -->
      </ng-template>
    
      <!-- User entails user details -->
      <ng-template #userDetails>
        <!-- TODO -->
      </ng-template>
    </div>

home.component.ts主页.component.ts

    import { Component, VERSION, TemplateRef, ViewChild } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      name = 'Angular ' + VERSION.full;
      
      templates: TemplateRef<any>[] = [];
      activeTemplate: TemplateRef<any>;
      
      constructor(private templateRef: TemplateRef<any>) {
        this.templates = [
          this.templateRef.allServices, // Error occurs
          this.templateRef.worker, // Error occurs
          this.templateRef.calendar, // Error occurs
          this.templateRef.userDetails // Error occurs
        ];
        this.activeTemplate = this.templates[0];
      }
      
    }

You can see how I am trying to pull out each reference but this does not work.您可以看到我是如何尝试提取每个参考文献的,但这不起作用。 I should also say I am very new to Angular and I read the documentation for TemplateRef from angular and to my understanding ElementRef is supposed to be the reference of the template.我还应该说我是 Angular 的新手,我从 angular 阅读了TemplateRef的文档,据我所知, ElementRef应该是模板的参考。

When you inject in constructor templateRef this is about your component home.component.当您在构造函数 templateRef 中注入时,这是关于您的组件 home.component 的。 You need get the templates using ViewChild您需要使用 ViewChild 获取模板

ViewChild('allServices',{static:true}) template0:TemplateRef<any>
ViewChild('worker',{static:true}) template1:TemplateRef<any>
ViewChild('calendar',{static:true}) template2:TemplateRef<any>
ViewChild('userDetails',{static:true}) template3:TemplateRef<any>

And use并使用

index=0;
this.activeTemplate = this['template'+index];

butyou asign the variable activeTemplate in ngOnInit (or in ngAfterViewInit if you don't use {static:true} ) not in constructor但是你在 ngOnInit 中分配变量ngOnInit (如果你不使用{static:true}则在 ngAfterViewInit 中)而不是在构造函数中

see stackblitz参见stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM