如何将EventEmitter与动态组件结合在一起?

眼豆豆

我正在尝试结合动态组件(在运行时创建)和EventEmitter概念来访问Angular 8中父组件中子组件的数据。

我的计划是创建一个功能,用户可以在其中动态添加元素(例如,仪表板上的卡片),也可以将其删除。在这种情况下,创建的卡具有“删除”按钮。应该使用此删除按钮将信息传播到父组件,以便可以从保存动态创建的组件的数组中删除子组件。

从角度文档中阅读了本教程,我需要创建一个指令。现在,我感到很惊讶,指令位于父组件和子组件之间,我不知道如何正确地发出Event以便从提到的数组中删除子组件。


指令

@Directive({
  selector: '[appCards]'
})
export class CardDirective {

  constructor(public viewContainerRef: ViewContainerRef) {
  }

  @Output() directiveDelete = new EventEmitter<any>();
}

父组件

card-banner.component.ts

@Component({
  selector: 'app-card-banner',
  templateUrl: './card-banner.component.html',
  styleUrls: ['./card-banner.component.scss']
})
export class CardBannerComponent implements OnInit, OnDestroy  {

  constructor(private componentFactoryResolver: ComponentFactoryResolver) { }

  @Input() cards: CardItem[];

  @ViewChild(CardDirective) appCards: CardDirective;

  loadCards() {
    const viewContainerRef = this.appCards.viewContainerRef;
    viewContainerRef.clear();
    for (const card of this.cards) {
      const componentFactory =
        this.componentFactoryResolver.resolveComponentFactory(card.component);
      const componentRef = viewContainerRef.createComponent(componentFactory);
      (componentRef.instance as CardContentComponent).data = card.data;
    }
  }

  addCard() {
    this.cards.push(new CardItem(CardContentComponent, {name: 'Card Dynamisch'}));
    this.loadCards();
  }

  removeLastCard() {
    this.cards.pop();
    this.loadCards();
  }

  onDelete(deleteBool: any) {
    console.log(deleteBool);
    console.log('delete in card-banner');
  }

  ngOnInit() {this.loadCards();
  }

  ngOnDestroy(): void {
  }

}


card-banner.component.html

<button (click)="addCard()" class="btn">Add Card</button>
<button (click)="removeLastCard()" class="btn">Remove Card</button>

<div style="margin: auto;">
  <ng-template appCards (directiveDelete)="onDelete($event)"></ng-template>
</div>

子组件

card-content.component.ts

@Component({
  selector: 'app-card',
  templateUrl: './card-content.component.html',
  styleUrls: ['./card-content.component.scss']
})
export class CardContentComponent implements CardInterfaceComponent {

  @Input() data: any;
  @Output() delete = new EventEmitter<any>();

  removeCard() {
    this.delete.emit(true);
    console.log('delete card: '  + this.data.name);
  }
}

card-content.component.html

<div >
  <div style="display: inline;">{{data.name}} <button (click)="removeCard()" class="btn">Delete</button></div>
</div>

我也有一个Card-Service,一个Card-Interface和一个Card-Item类,但是我认为它们在这种情况下没有影响力,因此我没有发布它们。如果有必要,我可以添加它们。


因此,我的问题是,父组件未收到子组件的删除消息,因此无法删除卡。

我希望有人可以帮助我了解信息丢失的位置以及在这种情况下应如何使用EventEmitter。

先感谢您!

布茨科夫斯基

动态创建组件后订阅组件事件:

  loadCards() {
    const viewContainerRef = this.appCards.viewContainerRef;
    viewContainerRef.clear();
    for (const card of this.cards) {
      const componentFactory = this.componentFactoryResolver.resolveComponentFactory(card.component);
      const componentRef = viewContainerRef.createComponent<CardContentComponent>(componentFactory);
      componentRef.instance.data = card.data;
      componentRef.instance.delete.subscribe(() => {
        // handle delete logic
      });
    }
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将架构组件与Android上的数据绑定结合在一起?

如何将typedef的结构与实例创建结合在一起?

如何将“最终形式计算”与“最终形式数组”结合在一起

如何将原位转换和复制转换结合在一起?

如何将5个以上的PDF与imagemagick转换结合在一起?

如何将聚合查询与不同联接结合在一起?

如何将数据框总结为与ID结合在一起的列表?

如何将python asyncio与线程结合在一起?

如何将Bash的流程替换与HERE文档结合在一起?

如何将PrettyPrintingJsonGeneratorDecorator和MaskingJsonGeneratorDecorator结合在一起?

如何将back_inserter与转换结合在一起,C ++

如何将Axios调用循环与等待功能结合在一起?

如何将时基轮询与等待的任务结合在一起

如何将分组的输入与对齐的表单结合在一起?

如何将文字信息与系统信息结合在一起?

如何将RequireJS路径和require-css结合在一起?

如何将输入与字符串结合在一起?

如何将.toggleClass()与.appendTo()结合在一起?

如何将这两个查询结合在一起?(MySQL)

如何将列与R中的条件结合在一起?

如何将DataTables与其他匹配器结合在一起?

R:如何将几个数据集结合在一起

如何将NavigationDrawerPageSlidingTabStrip与StickyListHeaders库结合在一起?

如何将“ IF FOR批处理”与“ FOR DELIMS批处理”结合在一起?

如何将JavaScript代码和html代码结合在一起?

Python:如何将for循环和while循环结合在一起?

将吸气剂结合在一起

如何将两个下一个结合在一起

如何将两个数据框与R中的项目数量结合在一起?