How to instantiate angular components with new keyword?
Imagine following as a template less component:
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
})
export class MyComponent {
constructor(private myService: MyService)
}
I know that angular components are only instantiated when it finds a selector tag in the template.
But... How can I instantiate the above with new keyword? Cause my components doesn't require a template.
But it should be an angular component with @component
decorator. (cause I need the feature of dependency injection)
But the problem is that when I create a new component like the following:
const comp = new MyComponent()
It also requires the service instance also to be passed to it's constructor while instantiating. Like this"
const comp = new MyComponent(new MyService())
It becomes even harder when that service we are passing in again depends on other services.
Like this...
const comp = new MyComponent(new MyService(new AnotherService(new YetAnotherService(... so on))))
So is there a workaround this?
Angular's DI is nice. Since we don't have any issues like the above. It automatically creates all the injected services. So is that possible with new
keyword.
NOTE I tried using plain typescript classes. But I still don't like it. I want to use angular components itself.
Is that possible?