DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems
  • Faster Releases With DevOps: Java Microservices and Angular UI in CI/CD

Trending

  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Why DDoS Protection Is an Architectural Decision for Developers
  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  1. DZone
  2. Coding
  3. Frameworks
  4. Angular Basics: Inject a Service/DI With Constructor in the Main.ts File

Angular Basics: Inject a Service/DI With Constructor in the Main.ts File

By 
RAkshiT ShaH user avatar
RAkshiT ShaH
·
May. 26, 20 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
8.4K Views

Join the DZone community and get the full member experience.

Join For Free

Well, the main.ts is the entry point for the Angular application. Here, we're not able to query instances of services or get them injected because we're outside of the Angular application. It looks like that storage object is completely decoupled from the Angular application as well, so any dependency injection you're after you'd have to do yourself.

Also of note: while I can’t find any explicit documentation on this, Amazon’s example of this Storage class has all of its properties as static, so I’m not sure if the configuration is expecting an instance of a class at all.

One thought I had would be to hijack APP_INITIALIZER in order to get DI tokens when the app is first bootstrapping. However, I am not familiar with Amplify, so I don't know when it needs to be configured, so this may or may not work:

File: App.module.ts

TypeScript
 




x


 
1
providers: [
2
  {
3
    provide: APP_INITIALIZER,
4
    useFactory: amplifyConfigFactory,
5
    deps: [CookieService],
6
    multi: true
7
  }
8
]



amplifyConfigFactory.factory.ts

TypeScript
 




xxxxxxxxxx
1


 
1
export function amplifyConfigFactory(cookieService: CookieService) {
2
  Amplify.configure({
3
    // ....
4
    storage: new MyStorage(cookieService)
5
  });
6

          
7
  return () => Promise.resolve(); // factories must return a Promise
8
}



so, above our deps entries get instantiated and injected into the factory in order, and we can create a MyStorage with the cookieService from the Injector.

Again, if the properties on MyStorage must be static, you’re going to need a different solution here. It seems to me like MyStorage should not be something known to the Angular application, but depending on whether or not Amplify needs to be configured before the APP_INITIALIZER hook, this could work.

If you have any issues, feel free to comment below.

AngularJS

Published at DZone with permission of RAkshiT ShaH. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems
  • Faster Releases With DevOps: Java Microservices and Angular UI in CI/CD

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook