javascript - Angular2: Creating child components programmatically -
question
how create child components inside parent component , display them in view afterwards using angular2? how make sure injectables injected correctly child components?
example
import {component, view, bootstrap} 'angular2/angular2'; import {childcomponent} './childcomponent'; @component({ selector: 'parent' }) @view({ template: ` <div> <h1>the children:</h1> <!-- ??? 3 child views shall inserted here ??? --> </div>`, directives: [childcomponent] }) class parentcomponent { children: childcomponent[]; constructor() { // when creating children, constructors // shall still called injectables. // e.g. constructor(childname:string, additionalinjectable:someinjectable) children.push(new childcomponent("child a")); children.push(new childcomponent("child b")); children.push(new childcomponent("child c")); // how create components correctly? } } bootstrap(parentcomponent);
edit
i found dynamiccomponentloader
in api docs preview. following error when following example: there no dynamic component directive @ element 0
this not approach take. instead rely on databinding against array render out more child components objects added backing array. child components wrapped in ng-for
i have example here similar in renders dynamic list of children. not 100% same, seems concept still same:
http://www.syntaxsuccess.com/viewarticle/recursive-treeview-in-angular-2.0
Comments
Post a Comment