marvin ralph
marvin ralph

Reputation: 1280

How to inject component into a div selector in angular2

I would like to know if there is anyway for me to inject a component into a div selector () using either a class or id. I know how to inject a component into another component but don't know how to go about it with injecting it into a div selector.

Upvotes: 1

Views: 3200

Answers (1)

estacks
estacks

Reputation: 104

You can use ViewContainerRef to inject components or templates into containers, which can be selected using @ViewChild by either the tag or an ID.

This blog post goes over a couple different ways to inject a template into a view container.

Understanding ViewContainerRef in Angular 2: https://netbasal.com/angular-2-understanding-viewcontainerref-acc183f3b682

There's another post that details creating dynamic components that are injected into a parent component, which can also be done using an element's ID.

Dynamically Creating Components With Angular: https://netbasal.com/dynamically-creating-components-with-angular-a7346f4a982d

Upvotes: 1

Related Questions