Original title: Can't initialize dynamically appended (HTML) component in Angular 2
I've created a directive that appends a modal to the body on initialization. When a button (with the directive injected into it) is clicked this modal fires up. But I want the contents of this modal to be another component (In fact I want the modal to be the component). It seems that I can't initialize the component.
Here is a plunker of what I've done:
http://plnkr.co/edit/vEFCnVjGvMiJqb2Meprr?p=preview
I'm trying to make my-comp the template of my component
'<div class="modal-body" #theBody>'
+ '<my-comp></my-comp>' +
'</div>
update for 2.0.0 final
Plunker example >= 2.0.0
Hint
If one application change the state in
SharedService
or calls a method that causes anObservable
to emit a value and the subscriber is in a different application then the emitter, the code in the subscriber is executed in theNgZone
of the emitter.Therefore when subscribing to an observable in
SharedService
useFor more details how to trigger change detection see Triggering Angular2 change detection manually
original
Dynamically added HTML is not processed by Angular and doesn't result in components or directives to be instantiated or added.
You can't add components outside Angulars root component (AppComponent) using
(deprecated)DynamicComponentLoader
ViewContainerRef.createComponent()
(Angular 2 dynamic tabs with user-click chosen components).I guess the best approach is to create a 2nd component outside Angulars root component is to call
bootstrap()
on each and use a shared service to communicate:Plunker example beta.17
Plunker example beta.14