简体   繁体   English

如果可能,如何使用循环动态渲染子组件? (角度 8)

[英]How to Render Child Components Dynamically using loops if possible? (Angular 8)

I am trying to render my angular components dynamically using loops but I'm lost.我正在尝试使用循环动态渲染我的 angular 组件,但我迷路了。

What I want to achieve is drag and re-arrange my components (I was able to achieve this using Dragula - ng2-dragula) but I want to save the arrangement to my local storage or session storage.我想要实现的是拖动并重新排列我的组件(我可以使用 Dragula - ng2-dragula 来实现),但我想将排列保存到我的本地存储或 session 存储中。

The tricky part is I am using child components棘手的部分是我正在使用子组件

<section [dragula]="'VAMPIRES'">
    <div class="first_element_class" id="first_element">
       <my-first-component
       [outputData]="outputData"></my-first-component>
    </div>

    <div class="second_element_class" id="second_element">
       <my-second-component
       [outputData]="outputData"></my-second-component>
    </div>
</section>

I tried using the DOM sanitizer pipe to render them via for loops我尝试使用 DOM sanitizer pipe 通过 for 循环渲染它们

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Pipe({
    name: 'trustHtml'
})
export class TrustHtmlPipe implements PipeTransform  {    
   constructor(readonly sr: DomSanitizer){}  

   transform(html: string) : SafeHtml {
      return this.sr.bypassSecurityTrustHtml(html); 
   } 
} 

Updated HTML Code (Let's assume I added my html elements to an array of object)更新了 HTML 代码(假设我将 html 元素添加到对象数组中)

objectHTML: any[] = [{ 
    toBeRender: `<div class="first_element_class" id="first_element">
       <my-first-component
       [outputData]="outputData"></my-first-component>
    </div>` 
}];

<section [dragula]="'VAMPIRES'">
    <div *ngFor="let element of objectHTML">
       <div [innerHTML]="element.toBeRender | trustHtml" ></div>
    </div>
</section>

My question is is it really possible to render the html child components/elements using ngFor?我的问题是真的可以使用 ngFor 渲染 html 子组件/元素吗? I tried using the DOMSanitizer but I'm only getting a blank page (No error on console)我尝试使用 DOMSanitizer 但我只得到一个空白页(控制台上没有错误)

I have a similar solution for you.我有一个类似的解决方案给你。 Firstly, in your <my-first-component> ts file declare "outputData" as @Input and use ngFor in HTML.首先,在您的<my-first-component> ts 文件中将“outputData”声明为@Input并在 HTML 中使用 ngFor。 Here is my sample code below.下面是我的示例代码。
CUSTOM Control自定义控制
HTML: event.component.html HTML:event.component.html

<h2>Event {{ event.id }}</h2>
<button (click)="event.hidden = true">Close me</button>


TS:event.component.ts TS:event.component.ts

import { Component, Input } from '@angular/core';
import { Event } from './event';
@Component({
selector: 'my-event',
templateUrl: './event.component.html'
})
export class EventComponent  {
@Input() event: Event;
}
export interface Event 
{
id: number;  
hidden: boolean;
}

**HTML:** app.component.html (You can use that code where you need to.) **HTML:** app.component.html(您可以在需要的地方使用该代码。)
import { Component } from '@angular/core';
import { Event } from './event';
@Component({
   selector: 'my-app',
   templateUrl: './app.component.html',
   styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
events: Array<Event> = [1, 2, 3, 4, 5].map(i => ({id: i, hidden: true}));
addEvent() {
    const event = this.events.find(e => e.hidden);
    if (event) {
    event.hidden = false;
    }
  }
}

Note: Here [event] is the @Input for your custom control and *ngFor is the loop.注意:这里 [event] 是您的自定义控件的 @Input, *ngFor 是循环。
TS: app.component.ts TS: app.component.ts

 import { Component } from '@angular/core'; import { Event } from './event'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent { events: Array<Event> = [1, 2, 3, 4, 5].map(i => ({id: i, hidden: true})); addEvent() { const event = this.events.find(e => e.hidden); if (event) { event.hidden = false; } } }

Note: Please check the code and let me know.注意:请检查代码并告诉我。 This code is also available in Stackblitz LINK .此代码也可在 Stackblitz LINK中找到。

You can iterate with ngFor and render many times the child component by his selector.您可以使用 ngFor 进行迭代,并通过其选择器多次渲染子组件。

 components = [1, 2, 3, 4, 5];
 <ng-container *ngFor="let component of components"> <child-component></child-component> </ng-container>

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

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