Hiras Haris
Hiras Haris

Reputation: 498

renderer over native element methods?

this is not a code-based question moreover this is theoretical, can anyone explain to me "Why would use renderer methods instead of using native element methods"? in angular. I have searched but I can't get a proper idea.

Upvotes: 4

Views: 738

Answers (2)

There is Security Reason For this

According to Angular docs Security:

Use this API as the last resort when direct access to DOM is needed. Permitting direct access to the DOM can make your application more vulnerable to XSS attacks.

Alternatively, you take a look at Renderer which provides API that can safely be used even when direct access to native elements is not supported.

So Render2 is always prefered over directly manipulating DOM using nativeElement

Upvotes: 1

Nugu
Nugu

Reputation: 848

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.

Read More

Upvotes: 1

Related Questions