damirarh / damirscorner-utterances

utteranc.es comments for https://damirscorner.com
0 stars 0 forks source link

/blog/posts/20170526-DynamicDependencyInjectionInAngular.html #51

Open damirarh opened 1 year ago

damirarh commented 1 year ago

Imported

URL: https://www.damirscorner.com/blog/posts/20170526-DynamicDependencyInjectionInAngular.html

damirarh commented 1 year ago

Imported comment written by David G on 2018-09-13T18:27:40

The URL for Factories with DI in AngularJS 6 is https://angular.io/guide/de...

damirarh commented 1 year ago

Imported comment written by Damir Arh on 2018-09-14T19:32:16

Thanks. I updated the link in the post.

damirarh commented 1 year ago

Imported comment written by mimmpelus on 2019-01-04T19:05:53

disqus_Jpo6eB6Eyo I do not agree

damirarh commented 1 year ago

Imported comment written by Andrew HB on 2019-02-20T14:48:35

Great post explained things very clearly, thank you!

damirarh commented 1 year ago

Imported comment written by Rhythm Khanna on 2019-04-24T14:03:26

What if I want to toggle between services while the app is running. I am using a boolean variable that i am toggling from the UI. When it is false, I want service 1 and when it is true, I want the service 2.

Your help and guidance will be appreciated a lot. Thank you

damirarh commented 1 year ago

Imported comment written by Damir Arh on 2019-04-25T19:40:36

I don't think this is directly supported in Angular's DI. However, you could use the Strategy pattern. You would implement three services:
- Service1 and Service2 would be the two different services from which you want to choose at runtime. They would both implement the same interface, i.e. the Strategy interface.
- The third service would be the Context. It would implement the same interface but would also expose the Boolean flag to choose the service to use. All of its members implementing the interface would simply call the corresponding member in the currently selected service.

You would register all three services with Angular's DI. You would inject Service1 and Service2 in the third service. You would inject the third service everywhere else where you wanted to use one of the other two services or switch between them.

damirarh commented 1 year ago

Imported comment written by Rhythm Khanna on 2019-04-29T09:26:03

What I am trying to do is create a custom ErrorHandler that I can toggle. When true, it should throw errors on the console and when false, it should put that on the custom function that i have created.
How do i implement Strategy pattern in this scenario.

damirarh commented 1 year ago

Imported comment written by Rhythm Khanna on 2019-05-02T09:22:40

I found a rather simple solution. While searching for the way to implement strategy method, I realized that I can simple throw the error in the custom handler once I use the error in the API.
This way the error shows up in the console as well as it is used.
Problem solved.
Thanks for the heads up.

damirarh commented 1 year ago

Imported comment written by Damir Arh on 2019-05-10T07:34:30

I explained the Strategy pattern approach in more detail in another blogpost.

damirarh commented 1 year ago

Imported comment written by Rodrigo on 2019-08-23T14:30:14

Hi!
I'm having a hard time understanding how to organize all this code between files. Could someone show me a example of how it could be done?

damirarh commented 1 year ago

Imported comment written by Damir Arh on 2019-08-23T19:44:03

I created a repository with a working Ionic 3 application based on (slightly updated) code from this post.

damirarh commented 1 year ago

Imported comment written by Rodrigo on 2019-08-23T20:21:50

Thank you very much! It's all clear now!

damirarh commented 1 year ago

Imported comment written by Rodrigo on 2019-08-26T14:10:38

Hi again!
Is there really the need to create this structure using the api.module.ts?
I tried to implement this solution using just services but I keep getting an error stating that the method I'm trying to call is not a function.
I'm calling the ngrx Store to check some value inside the factory function. I think that's the problem.

damirarh commented 1 year ago

Imported comment written by Damir Arh on 2019-08-26T19:25:15

The configuration object and a separate module aren't required. The first section in this post describes such an approach. You can also see it working in a previous commit in the repository above.

I'm not familiar with ngrx so I can't comment on that.