随笔 - 65  文章 - 0  评论 - 3  阅读 - 10万

Angular动态组件

一、主页面:

app.component.html:

1  <button (click)="load();">动态</button>
2 2<div #domRoom><!--动态组件加载到这个div里--></div>
View Code

 

app.component.ts:

复制代码
 1 import {Component, ViewContainerRef, ViewChild, ComponentFactoryResolver} from '@angular/core';
 2 import {DynamicdialogComponent} from "./dynamicdialog/dynamicdialog.component";
 3 
 4 @Component({
 5   selector: 'app-root',
 6   templateUrl: './app.component.html',
 7   styleUrls: ['./app.component.css']
 8 })
 9 export class AppComponent {
10 
11   @ViewChild('domRoom', {read: ViewContainerRef}) domRoom: ViewContainerRef;
12 
13   constructor(public cfr: ComponentFactoryResolver,){}
14 
15   load(){
16     let com = this.cfr.resolveComponentFactory(DynamicdialogComponent);
17     let comref = this.domRoom.createComponent(com);
18     let data = [];
19     comref.instance.sourseData  = data;  //创建动态组件传输数据
20     comref.instance.myWorkOver.subscribe((msg) => { // 订阅组件传出事件 判断执行销毁组件操作
21       if (msg) {
22         this.domRoom.clear();
23       }
24     });
25   }
26 }
View Code
复制代码

 

二、动态组件:

dynamicdialog.component.html:

1 <p-dialog header="动态组件" [(visible)]="display" modal="modal" width="850" [responsive]="true" (onHide)="dialogHide()">
2  666
3 </p-dialog>
View Code

 

dynamicdialog.component.ts:

复制代码
 1 import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
 2 
 3 @Component({
 4   selector: 'app-dynamicdialog',
 5   templateUrl: './dynamicdialog.component.html',
 6   styleUrls: ['./dynamicdialog.component.css']
 7 })
 8 export class DynamicdialogComponent implements OnInit {
 9 
10   @Input() sourseData: any; // 外部传入的数据源
11   @Output() myWorkOver: EventEmitter<boolean> = new EventEmitter<boolean>(); // 用于向外传事件(根据实际业务,也可以改为EventEmitter<any>)
12   public display: boolean = true; // 默认显示dialog
13 
14   constructor() { }
15 
16   ngOnInit() {
17   }
18 
19   dialogHide() { // dialog hide调用 默认认为当dialog 关闭了 就销毁组件 实际业务按需求综合考虑
20     this.myWorkOver.emit(true); // 向外传播事件 并将消息体传播出去
21   }
22 
23 }
View Code
复制代码

 

app.module.ts:

复制代码
 1 import { BrowserModule } from '@angular/platform-browser';
 2 import { NgModule } from '@angular/core';
 3 import { DataTableModule } from 'primengcn/primeng';
 4 import { AppRoutingModule } from './app-routing.module';
 5 import {DialogModule} from 'primengcn/components/dialog/dialog';
 6 import { AppComponent } from './app.component';
 7 import { DynamicdialogComponent } from './dynamicdialog/dynamicdialog.component';
 8 import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
 9 
10 
11 @NgModule({
12   declarations: [
13     AppComponent,
14     DynamicdialogComponent
15   ],
16   imports: [
17     BrowserModule,
18     AppRoutingModule,
19     DialogModule,
20     DataTableModule,
21     BrowserAnimationsModule
22   ],
23   entryComponents: [
24     DynamicdialogComponent
25   ],
26   providers: [],
27   bootstrap: [AppComponent]
28 })
29 export class AppModule { }
View Code
复制代码

 

posted on   陌生街中吹起褪色故梦  阅读(165)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示