Why would you use renderer methods instead of using native element methods?

Technology CommunityCategory: AngularWhy would you use renderer methods instead of using native element methods?
VietMX Staff asked 3 years ago

Angular is a platform, and the browser is just one option for where we can render our app. When we access the native element directly we are giving up on Angular’s DOM abstraction and miss out on the opportunity to be able to execute also in none-DOM environments such as:

  • native mobile,
  • native desktop,
  • web worker
  • server side rendering.

The Renderer2 class is an abstraction provided by Angular in the form of a service that allows to manipulate elements of your app without having to touch the DOM directly. This is the recommended approach because it then makes it easier to develop apps that can be rendered in environments that don’t have DOM access, like on the server, in a web worker or on native mobile.