Angular — NgComponentOutlet input bindings

Nhan Nguyen
2 min readDec 8, 2023

--

You might not be familiar with ngComponentOutlet in the first place, so let’s explain what the directive does

Let’s consider the following template syntax:

The above code would load a dynamic component into the ng-container. Assigning a new component type to componentTypeExpression would display it in that container.

Here’s a basic example that shows a HelloComponent in the component outlet:

This can be helpful if you need to use different types of components and don’t want to (or can not) use the router to make it happen.

This feature of Angular allows us to pass input values to that component.

Let’s assume that HelloComponent looks like this:

We can bind a value to the name input as follows:

And that does the trick! You can see an example in action on Stackblitz here.

“inputs” is an object that can have as many keys as needed. In my example, there is only one: name.

I hope you found it useful. Thanks for reading. 🙏

Let’s get connected! You can find me on:

--

--

Nhan Nguyen
Nhan Nguyen

Responses (1)