1

I'd like to roll an array of objects and use the properties from each index as input parameters of Angular2 components Id like to add to the dom

Bc these components are recursive, I need to use dynamiccomponentloader's function inside of a loop that traverses this array of objects (instead of *ngFor)

I am looking for an example that puts the component's template markup inside of a li (which is inside of a ul).

I am not certain how loadAsRoot, loadIntoLocation, or, loadNextToLocation can append a new li inside of a ul instead of just replacing its previous contents

I would be most grateful for a plnkr or fiddle to demonstrate this. Thank you in advance for your help

Benjamin McFerren
  • 822
  • 5
  • 21
  • 36

1 Answers1

1

Plunker example beta.17
Plunker example beta.9

import {Component, DynamicComponentLoader, ElementRef, Input} from 'angular2/core'

@Component({
  selector: 'dyn-cmp',
  template: `
  <h2>Dynamic {{name}}</h2>
`
})
export class DynCmp {
  @Input() name:string;
}

@Component({
  selector: 'my-app',
  directives: [],
  template: `
  <h2>Hello {{name}}</h2>
  <div #target></div>
`
})
export class App {
  list = ['a', 'b', 'c'];
  constructor(private dcl:DynamicComponentLoader, private elRef:ElementRef) {
    this.name = 'Angular2';
  }  

  ngAfterViewInit() {
    this.list.forEach((val) => {
      this.dcl.loadIntoLocation(DynCmp, this.elRef, 'target')
      .then(cmpRef => cmpRef.instance.name = val);
    });
  }
}

You can't use loadAsRoot() because it removes the target and than can't be reused to add another component.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • why are you not using this in your template property? 'Parent ( ... )' – Benjamin McFerren Mar 22 '16 at 19:56
  • Not sure what you mean. What should `Parent(...)` be or do? – Günter Zöchbauer Mar 22 '16 at 20:31
  • I do not know what Parent(...) is supposed to do. I only ask because I see it on the Angular2 page and from your plnkr, it doesn't appear that you need it. https://angular.io/docs/ts/latest/api/core/DynamicComponentLoader-class.html What is the purpose of Parent(...) ? – Benjamin McFerren Mar 22 '16 at 20:52
  • 1
    A bit late but anyway. `Parent` is just plain text in HTML to show something. – Günter Zöchbauer Apr 26 '16 at 12:22
  • In that new Plunker example beta.17 that you posted, how would you perform cleanup if you invoked a function that looked like this? pushD() { this.list.push('d'); ) or this popper() { this.list.pop(); } https://plnkr.co/edit/Nbfu7M?p=preview – Benjamin McFerren May 03 '16 at 05:53
  • The beta.17 example in http://stackoverflow.com/a/36325468/217408 shows how to dispose (`destory()` in beta.17) a dynamically created component. If you use such a wrapper you can use `ngOnDestroy()` to destroy the component when the wrapper is removed. Without the wrapper you would need to maintain a list of `cmpRef` to call `destroy()` on when you remove one from the list. – Günter Zöchbauer May 03 '16 at 06:02
  • So if I am attempting to display a
    • ...
    • ...
    inside of my parent view, I have two choices?(1) that each component represented in the
      ...
    will "self-destruct" (2) parent iterating over an array/object full of cmpRef references and invokes each element's cmpRef object's destroy() function
    – Benjamin McFerren May 03 '16 at 15:30
  • In your 17 example, a master parent instantiates the DCL wrapper with html tags. What kills(destroys) the DCL wrapped child component instance if the master parent itself uses DynamicCompnentLoader to instantiate its DCL wrapped child? – Benjamin McFerren May 03 '16 at 15:35
  • I don't get this part "if the master parent itself uses DynamicCompnentLoader to instantiate its DCL wrapped child?" – Günter Zöchbauer May 03 '16 at 15:37
  • another way to say it - it looks like you've shown how to represent an individual view with DCL but how do you represent (and maintain) multiple components with DCL? Do you have a plnkr with this option: "maintain a list of cmpRef to call destroy() on when you remove one from the list" ? – Benjamin McFerren May 03 '16 at 15:39
  • for example, if I insert or delete at a particular index in my cmpRef array, or I .shuffle() the entire array, how is that change represented with the actual html dom elements? automatic change/rearrange? – Benjamin McFerren May 03 '16 at 15:40
  • The `` instances are normal components maintained by `*ngFor`. When `*ngFor` removes a component it calls `ngOnDestroy()` which calls `cmpRef.destory()`. Is that what you mean? – Günter Zöchbauer May 03 '16 at 15:41
  • If you maintain `cmpRef` then you are fully responsible. You have to take care of everything yourself. – Günter Zöchbauer May 03 '16 at 15:42