当前位置: 首页 > 面试题库 >

动态添加和删除角度组件

康烨伟
2023-03-14
问题内容

当前的官方文档仅显示如何动态 更改 标签 的组件<ng-template>

我想实现的是,让我们说我有3个部分组成:headersection,并footer具有以下选择:

<app-header>
<app-section>
<app-footer>

再有6个按钮,这将增加或删除每个组件:Add HeaderAdd Section,和Add Footer

当我单击时Add Header,该页面将添加<app-header>到呈现它的页面中,因此该页面将包含:

<app-header>

然后,如果我单击Add Section两次,该页面现在将包含:

<app-header>
<app-section>
<app-section>

如果单击Add Footer,该页面现在将包含所有这些组件:

<app-header>
<app-section>
<app-section>
<app-footer>

可以在Angular中实现吗?请注意,这ngFor不是我要寻找的解决方案,因为它仅允许向页面添加相同的组件,而不是不同的组件。

编辑:ngIfngFor是不是我要找的模板已经预定的解决方案。我所寻找的是类似的堆栈component秒或一arraycomponentS其中,我们可以添加,删除和更改的任何索引array容易。

编辑2:为了更清楚,让我们有一个为什么ngFor不起作用的另一个例子。假设我们具有以下组件:

<app-header>
<app-introduction>
<app-camera>
<app-editor>
<app-footer>

现在出现了一个新组件,<app-description>用户希望在和之间插入该组件<app-editor>ngFor仅当有一个我想一遍又一遍地循环的组件时,它才起作用。但是对于不同的组件,ngFor此处失败。


问题答案:

您可以尝试实现以下目标:通过使用动态创建组件ComponentFactoryResolver,然后将其注入ViewContainerRef。动态执行此操作的一种方法是将组件的类作为将创建和注入组件的函数的参数传递。

请参见下面的示例:

import {
  Component,
  ComponentFactoryResolver, Type,
  ViewChild,
  ViewContainerRef
} from '@angular/core';

// Example component (can be any component e.g. app-header app-section)
import { DraggableComponent } from './components/draggable/draggable.component';

@Component({
  selector: 'app-root',
  template: `
    <!-- Pass the component class as an argument to add and remove based on the component class -->
    <button (click)="addComponent(draggableComponentClass)">Add</button>
    <button (click)="removeComponent(draggableComponentClass)">Remove</button>

    <div>
      <!-- Use ng-template to ensure that the generated components end up in the right place -->
      <ng-template #container>

      </ng-template>
    </div>

  `
})
export class AppComponent {
  @ViewChild('container', {read: ViewContainerRef}) container: ViewContainerRef;

  // Keep track of list of generated components for removal purposes
  components = [];

  // Expose class so that it can be used in the template
  draggableComponentClass = DraggableComponent;

  constructor(private componentFactoryResolver: ComponentFactoryResolver) {
  }

  addComponent(componentClass: Type<any>) {
    // Create component dynamically inside the ng-template
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentClass);
    const component = this.container.createComponent(componentFactory);

    // Push the component so that we can keep track of which components are created
    this.components.push(component);
  }

  removeComponent(componentClass: Type<any>) {
    // Find the component
    const component = this.components.find((component) => component.instance instanceof componentClass);
    const componentIndex = this.components.indexOf(component);

    if (componentIndex !== -1) {
      // Remove component from both view and array
      this.container.remove(this.container.indexOf(component));
      this.components.splice(componentIndex, 1);
    }
  }
}

笔记:

  1. 如果您希望以后更轻松地删除这些组件,可以在局部变量中跟踪它们,请参见this.components。或者,您可以遍历内的所有元素ViewContainerRef

  2. 您必须将组件注册为输入组件。在模块定义中,将组件注册为entryComponent(entryComponents: [DraggableComponent])。



 类似资料:
  • 本文向大家介绍在Vue组件上动态添加和删除属性方法,包括了在Vue组件上动态添加和删除属性方法的使用技巧和注意事项,需要的朋友参考一下 如下所示: 在组件上添加属性 this.$set(this.data,"obj",value'); 删除属性this.$delete(this.data,"obj",value'); 以上这篇在Vue组件上动态添加和删除属性方法就是小编分享给大家的全部内容了,希望

  • 本文向大家介绍详解原生JS动态添加和删除类,包括了详解原生JS动态添加和删除类的使用技巧和注意事项,需要的朋友参考一下 由于需要, 给按钮组监听点击事件(要求用事件委托),当有一个按钮被点击时,相应的给该按钮添加一个类(激活类),其他没有点击的按钮就要移出该类 添加和和删除类有三种方法 首先等到一个 dom 对象(也叫dom元素), 通过document.getElement……的几种方法得到 如

  • 问题内容: https://jsfiddle.net/techboy0007/j1eas924/ https://i.stack.imgur.com/KXFot.png 问题答案: 您可以通过反应状态来实现。在您的情况下,您正在使用嵌套对象,因此在更新它们时需要格外小心。 突变您的想法不是一个好主意,因为它很容易导致错误或意外行为。 仔细查看这些功能的工作方式。 在这里,您有一个现场演示。 PD:

  • 问题内容: 我使用指令创建联系表格。最初我创建用于显示客户表单的customerForm指令。在这种形式下,我有一个按钮,当我们单击添加按钮时,称为getData函数,该函数内部使用newDirective显示ul列表。为此,我使用$ compile api编译html代码。很好,当我们单击“删除”按钮时,它也显示列表值和“删除”按钮,它称为scope.remove()函数。但是它只能删除一个。之

  • 本文向大家介绍JQuery动态添加和删除表格行的方法,包括了JQuery动态添加和删除表格行的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JQuery动态添加和删除表格行的方法。分享给大家供大家参考。具体分析如下: 昨天做页面表格行动态添加和删除,看了无数的介绍,发现了一个好东东,JQuery。用它实现起来还真的是很方便,这个是我用到我们平台的一个方法。 后台使用的也是比较容易的,

  • 我知道如何处理MenuItems的操作。我只需要动态网格窗格的帮助。 GridPanes有一个内置的函数addRow,也许它是一个可以使用的函数。我该怎么解决这个?谢谢!感谢所有的提示。