angular - How to target specific element with DynamicComponentLoader -


background

i have data represent list of folders , categories of documents; our information management software through web interface. raw list each item has parentfolderno i've created object children: any[] property - have nested list of items; this:

{   id: 123,   guid: "8dcaae38-4dcc-48f7-bd91-c8b0cb725890"   children: [     {       id: 234,       guid: "...."     },     {       id: 345,       guid: "...."       children: [...]     }   ] } 

implementation

there many more objects there, have it's own children, don't, each unique , has unique guid. object need create ui element lets user select specific folder or category , limit search. can think of selectable breadcrumbs. i've created component uses dynamiccomponentloader display specific 'tree' of data:

@component({   host: { "[attr.id]": "category.guid" },   selector: 'category-group',   template: `     <ul class="category__group">       <li>         <a (click)="select()">all categories</a>       </li>       <li [class.selected]="child.selected" *ngfor="#child of category.children">         <a *ngif="!child.selected" (click)="select(child)">{{ child.name }}</a>         <b *ngif="child.selected">{{ child.name }}</b>       </li>     </ul>   `, }) class category {   public category: any;    select(thecategory: any) {     this.category.children.map(subcategory => subcategory.selected = false);     if (thecategory) {       thecategory.selected = true;       // pass thecategory categoryselectcomponent       // create new category-group... works.     }   } }  @component({   selector: 'category-select',   template: `     <b>root</b>      <div #root></div>   `, }) export class categoryselectcomponent {   @input() root: any;    constructor(     private _dcl: dynamiccomponentloader,      private _eref: elementref,      private _inj: injector   ) {}    ngoninit() { this.create(this.root); }    create(parent: any) {     if (!parent.children) return;     this._dcl       .loadintolocation(category, this._eref, 'root')       .then(ref => ref.instance.category = parent)   } } 

this works, there's flaw - adds new <category-group>. when select different child need replace category-groups 'below' it. so:

is there way replace component dynamiccomponentloader?

  create(parent: any) {     if (!parent.children) return;     // guid":"8dcaae38-4dcc-48f7-bd91-c8b0cb725890"     this._dcl       .loadintolocation(category, this._eref, 'root')       .then(ref => ref.instance.category = parent)   } 

what can create() function functionality need? how can use element id="8dcaae38-4dcc-48f7-bd91-c8b0cb725890" in _dcl? tried other methods didn't manage make work...

thanks.

i'm having same problem (i'm new in angular2), if use loadintolocation() method won't replace existing template. if use loadasroot() (taking account point same selector. example:

this._dynamiccomponentloader.loadasroot(componenta, '#container', this._injector)                 .then((res) => {                     res.instance["label"] = "text1";                     compref.location.internalelement.parentview.changedetector.ref.detectchanges();                 }); 

and next time want replace content of componenta other template (let's say, original, componentb):

this._dynamiccomponentloader.loadasroot(componentb, '#container', this._injector)                 .then((res) => {                     res.instance["label"] = "text2";                     compref.location.internalelement.parentview.changedetector.ref.detectchanges();                 }); 

you see there strange line here:

compref.location.internalelement.parentview.changedetector.ref.detectchanges();

that line needed if want inform components listen changes in attributes when using loadasroot() method can see here:

https://github.com/angular/angular/issues/6223#issuecomment-193895253

hope helps.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -