Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass attributes to dynamic component #120

Closed
nickbullock opened this issue Apr 16, 2018 · 5 comments
Closed

Pass attributes to dynamic component #120

nickbullock opened this issue Apr 16, 2018 · 5 comments

Comments

@nickbullock
Copy link

nickbullock commented Apr 16, 2018

Hi! Thank you for this library.
How can i pass attributes(class for example) to dynamic components?

<ndc-dynamic [ndcDynamicComponent]="component"
                 [ndcDynamicInputs]="inputs"
                 [ndcDynamicOutputs]="outputs"
                 [classForDynamicComponent]="???"
    >
    </ndc-dynamic>
@gund
Copy link
Owner

gund commented Apr 19, 2018

Well unfortunately right now you have to do it yourself imperatively (not via html).

And the way you can set attributes is by using <ndc-dynamic> component to inject dynamic component and then grabbing it's reference in your host component via @ViewChild and then using it's componentRef public property which in turn will have location property of type ElementRef.

Than that can be used along with Renderer2 to set classes etc.

So here's example:

import { DynamicComponent } from 'ng-dynamic-component';

@Component({
  selector: 'my-component',
  template: `
    <ndc-dynamic [ndcDynamicComponent]="dynamicComp"></ndc-dynamic>
`
})
export class MyComponent implements AfterViewInit {
  dynamicComp = SomeComponent;
  @ViewChild(DynamicComponent) ndcDynamic: DynamicComponent;

  constructor(private renderer: Renderer2) {}

  ngAfterViewInit() {
    const dynamicElementRef = this.ndcDynamic.componentRef.location;
    this.renderer.setClass(dynamicElementRef.nativeElement, 'my-class');
  }
}

NOTE: You have to set your dynamic class only AfterViewInit because that's when angular will provide you requested ViewChild (it will not be set in OnInit hook).

@gund
Copy link
Owner

gund commented Apr 19, 2018

I think this is actually a pretty useful thing to be able to set attributes, and it would be really cool to be able to do so declaratively.

I have an idea how to make it possible to set via directive but will have do some experiments to validate because it should not be tied to neither <ndc-dynamic> nor DynamicDirective.

That way you can simply apply it on ngComponentOutlet without anything else =)

@gund gund added the question label Apr 19, 2018
@gund
Copy link
Owner

gund commented Apr 19, 2018

Btw for the time being you can create a more usable solution for yourself.

You can extend DynamicComponent with new component that will additionally accept a set of attributes and internally assign them using the method I described above.

And then when you import DynamicModule just pass new component as a second argument to withComponents() method so that it will be used instead of default <ndc-dynamic>.

@nickbullock
Copy link
Author

Thank you sir!

gund added a commit that referenced this issue Apr 23, 2018
That is working with ngComponentOutlet directive for right now

related #120
@gund
Copy link
Owner

gund commented Apr 24, 2018

Hey @nickbullock, if you want you can try a new directive added in new release that will allow to set attributes right from your templates.

Let me know how it works for you =)

@gund gund closed this as completed Apr 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants