Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 DynamicComponentLoader

Tags:

angular

Using any method (ex. loadIntoLocation) I can dynamically load a directive into my basic component to a specific container !

@Component({ ... template: ` <div #elementContainer>empty div</div>` ...})

then by calling

this._dcl.loadIntoLocation(DirectiveClass, this._elementRef, 'elementContainer')

I will pass to the empty div the template of a DirectiveClass like:

<input type="radio" name="gender" value="male" checked>

so the final result will be an empty div plus my input element!

The question is: how to change the value attribute or any other of the input value also dynamically by calling loadIntoLocation ?

like image 546
t v Avatar asked Jun 03 '26 10:06

t v


1 Answers

update

loadIntoLocation() was removed from DynamicComponentLoader. Use instead loadNextToLocation() with ViewContainerRef

export class AppComponent {
  @ViewChild('target', {read: ViewContainerRef}) target;
  top;

  constructor(private dcl:DynamicComponentLoader) {}
  ngAfterViewInit() {
    this.dcl.loadNextToLocation(DynamicComponent, this.target)
    .then(ref => {
      ref.instance.someEvent
      .subscribe(value => {
        this.top = value.clientX; 
      })
    });
  }
}

Plunker example beta.16

original

This looks quite similar to Angular 2 - How to set id attribute to the dynamically loaded component that I recently answered

You don't explain how you add the input.

this.dcl.loadIntoLocation(DirectiveClass, this._elementRef, 'elementContainer').then((cmp) => {
  cmp.location.nativeElement.querySelector('input').value = 'someId';
});

might do what you want (not tested).

like image 71
Günter Zöchbauer Avatar answered Jun 05 '26 00:06

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!