Reputation: 11
Assuming Project is an object class I created to be used in a component. Can I instantiate a class with the new
keyword
project = new Project();
AND a constructor?
constructor(public project: Project) {}
Are these the same? If they are the same, which is preferred?
Upvotes: 1
Views: 1190
Reputation: 10854
Can I instantiate a class with the new keyword?
Yes you can, but it will create an object that is not injectable.
Are these the same?
No, they are not the same. When you declare the object as an argument in the constructor that object is injectable and is declared as a dependency of that class in the Angular framework.
From the official Angular docs on Dependency Injection
In Angular, the DI framework provides declared dependencies to a class when that class is instantiated
Upvotes: 1