Use Existing provider practical example

1 minute read

As we know UseExisting provider configures the Injector to return a value of another UseExisting token. Through this provider, we can use already existing instance or object. Let’s discuss one practical example of UseExisting provider which is present in angular itself.

Inside the angular code base you will see library called as browser, inside that they have some implementation for sanitization which means they want to check whether security wise things are correct or not. Sanitizer is used by the views to sanitize potentially dangerous values. DomSanitizer and eDomSanitizerImpl are two subclasses used in Sanitizer.

So basically we have two abstract classes and one implementation class for sanitizer. In video session Use Existing Practical Example | useExisting | Angular | Tutorial we have explained practical example of UseExisting provider type in detail. This is the real world example that angular uses with in itself. So mostly UseExisting provider is framework related stuff that shows capability of both Sanitizer & domSanitizer.

DomSanitizer helps preventing Cross Site Scripting Security bugs (XSS) by sanitizing values to be safe to use in the different DOM contexts. In specific situations, it might be necessary to disable sanitization. Users can bypass security by constructing a value with one of the bypassSecurityTrust methods, and then binding to that value from the template. We can call one of the following methods depending upon the context, to disable Angular’s built-in sanitization.

  • bypassSecurityTrustHtml
  • bypassSecurityTrustScript
  • bypassSecurityTrustStyle
  • bypassSecurityTrustUrl
  • bypassSecurityTrustResourceUrl