gpt4 book ai didi

angular - 如何动态创建引导模式作为 Angular2 组件?

转载 作者:太空狗 更新时间:2023-10-29 16:57:29 32 4
gpt4 key购买 nike

原标题:Can't initialize dynamically appended (HTML) component in Angular 2

我创建了一个指令,在初始化时将模态附加到正文。单击按钮(其中注入(inject)了指令)时,将触发此模式。但是我希望这个模态的内容是另一个组件(实际上我希望模态是组件)。看来我无法初始化组件。

下面是我所做的一个 plunker:

http://plnkr.co/edit/vEFCnVjGvMiJqb2Meprr?p=preview

我正在尝试让 my-comp 成为我的组件的模板

 '<div class="modal-body" #theBody>' 
+ '<my-comp></my-comp>' +
'</div>

最佳答案

2.0.0 最终更新

Plunker example >= 2.0.0

@NgModule({
imports: [ BrowserModule ],
declarations: [ App, ModalComponent, CompComponent],
providers: [SharedService],
entryComponents: [CompComponent],
bootstrap: [ App, ModalComponent ]
})
export class AppModule{}
export class ModalComponent {
@ViewChild('theBody', {read: ViewContainerRef}) theBody;

cmp:ComponentRef;

constructor(
sharedService:SharedService,
private componentFactoryResolver: ComponentFactoryResolver,
injector: Injector) {

sharedService.showModal.subscribe(type => {
if(this.cmp) {
this.cmp.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(type);
this.cmpRef = this.theBody.createComponent(factory)
$('#theModal').modal('show');
});
}

close() {
if(this.cmp) {
this.cmp.destroy();
}
this.cmp = null;
}
}

提示

如果一个应用程序更改了 SharedService 中的状态或调用了一个导致 Observable 发出值的方法,而订阅者在不同的应用程序中,那么发射器,订阅者中的代码在发射器的 NgZone 中执行。

因此在 SharedService 中订阅一个可观察对象时使用

class MyComponent {
constructor(private zone:NgZone, private sharedService:SharedService) {
private sharedService.subscribe(data => this.zone.run() => {
// event handler code here
});
}
}

有关如何触发更改检测的更多详细信息,请参阅 Triggering Angular2 change detection manually

原创

动态添加的 HTML 不会被 Angular 处理,也不会导致组件或指令被实例化或添加。

您不能使用 DynamicComponentLoader 在 Angulars 根组件 (AppComponent) 之外添加组件(已弃用)ViewContainerRef.createComponent() ( Angular 2 dynamic tabs with user-click chosen components ) .

我想最好的方法是在 Angulars 根组件之外创建第二个组件,即在每个组件上调用 bootstrap() 并使用共享服务进行通信:

var sharedService = new SharedService();

bootstrap(AppComponent, [provide(SharedService, {useValue: sharedService})]);
bootstrap(ModalComponent, [provide(SharedService, {useValue: sharedService})]);

Plunker example beta.17
Plunker example beta.14

@Injectable()
export class SharedService {
showModal:Subject = new Subject();
}

@Component({
selector: 'comp-comp',
template: `MyComponent`
})
export class CompComponent { }


@Component({
selector: 'modal-comp',
template: `
<div class="modal fade" id="theModal" tabindex="-1" role="dialog" aria-labelledby="theModalLabel">
<div class="modal-dialog largeWidth" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="theModalLabel">The Label</h4></div>
<div class="modal-body" #theBody>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" (close)="close()">Close</button>
</div></div></div></div>
`
})
export class ModalComponent {
cmp:ComponentRef;
constructor(sharedService:SharedService, dcl: DynamicComponentLoader, injector: Injector, elementRef: ElementRef) {
sharedService.showModal.subscribe(type => {
if(this.cmp) {
this.cmp.dispose();
}
dcl.loadIntoLocation(type, elementRef, 'theBody')
.then(cmp => {
this.cmp = cmp;
$('#theModal').modal('show');
});
});
}

close() {
if(this.cmp) {
this.cmp.dispose();
}
this.cmp = null;
}
}


@Component({
selector: 'my-app',
template: `
<h1>My First Attribute Directive</h1>
<button (click)="showDialog()">show modal</button>
<br>
<br>`,
})
export class AppComponent {
constructor(private sharedService:SharedService) {}

showDialog() {
this.sharedService.showModal.next(CompComponent);
}
}

关于angular - 如何动态创建引导模式作为 Angular2 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36566698/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com