I would like to dynamically build a component tree basing on some information received from AJAX calls.
How to programmatically add a component to the DOM from inside of other component? I have <outer-comp>
and I would like, basing on some logic, insert an <inner-comp>
. The following code just inserts the elements <inner-comp></inner-comp>
to the DOM, and not actual <inner-comp>
representation.
@NgComponent(
selector: 'outer-comp',
templateUrl: 'view/outer_component.html',
cssUrl: 'view/outer_component.css',
publishAs: 'outer'
)
class AppComponent extends NgShadowRootAware {
void onShadowRoot(ShadowRoot shadowRoot) {
DivElement inner = shadowRoot.querySelector("#inner");
inner.appendHtml("<inner-comp></inner-comp>");
}
}
Update: I managed to render the inner component correctly in the following way, but I'm still not sure if this is the proper way:
class AppComponent extends NgShadowRootAware {
Compiler compiler;
Injector injector;
AppComponent(this.compiler, this.injector);
void onShadowRoot(ShadowRoot shadowRoot) {
DivElement inner = shadowRoot.querySelector("#inner");
inner.appendHtml("<inner-comp></inner-comp>");
BlockFactory template = compiler(inner.nodes);
var block = template(injector);
inner.replaceWith(block.elements[0]);
}
}
This would be a proper use of the block API.
Also, if you ever need to recompile the inner template make sure you do
childScope.$destroy()
on the previouschildScope
.I did finally get this to work but was not happy with having to add a timer:
The above code samples on longer work because of changes in the Angular Dart library. Specifically ViewFactory.call which no longer takes an injector but takes a Scope and a DirectiveInjector. I've tried adapting what's above and I get very close. The component shows up but none of the bindings are replaced (I see {{cmp.value}} for example.
Here's the code I'm using. I think the issue here is that DirectiveInjector is coming in as null.
The API has changed in AngularDart 0.9.9:
AngularDart 0.10.0 introduces these changes:
So the code of pavelgj now looks like so:
EDIT
The package http://pub.dartlang.org/packages/bwu_angular contains this decorator/directive as
bwu-safe-html
------
I use a custom directive for that
It can be used like
Angular issue
I created an issue to include this functionality into Angulars
ng-bind-html
https://github.com/angular/angular.dart/issues/742 (declined)