Understanding DOM shadowing using View Encapsulation in Angular2
Posted By : Satwinder Singh | 10-Sep-2018
Understanding View Encapsulation
View Encapsulation in Angular 2 is kind of used to restrict the access to some kind of data or simply just restrict the access to the data. Angular 2 is inheriting all the features of web component which were initially defined in the polymer like HTML import custom elements, DOM shadowing etc. Dom shadowing means we are actually trying to wrap up or we are trying to encapsulate the DOM inside the DOM which can be visible to the outside world or which can be isolated for that particular division. So, therefore, View Encapsulation is available in the Angular 2 components. There are three different types of view encapsulation that angular component can have – one is the default which is called emulated, other is native and the third one is none. The role of Dom shadowing is that it is a concept which enables us to isolate the styling of a particular component isolated from the outside world. For example, we can specify the styles in the @Component decorator of a particular component and we can use this component at a number of places in our Angular 2 app. But we want to apply these styles only to this particular component and we don’t want the styles applied in this component to effect any other part or component of the app. So Angular uses Dom shadowing using which we are encapsulating styles for this component only.
How does shadow dom work with view encapsulation?
So how is it happening? When we are using the default value for view encapsulation i.e. emulated and run our app in the browser and inspect our styles using developer’s tool, we can observe that some strange kind of attribute like “_ngcontent-
Though we can override this behavior provided by angular. To override this behavior we can add a property to our @Component decorator which is called encapsulation and set the value to ViewEncapulstion and now we can choose between three modes – Emulated, Native and None. Have a look on the code below to understand the component.ts file:-
import { component , OnInit , ViewEncapsulation } from ‘@angular/core’;
@Component({
selector: ‘xyz’,
templateUrl : ‘./xyz.component.html’,
stylesUrl: [‘./xyz.component.css’],
encapsulation: ViewEncapsulation.Emulated
})
export class XyzComponent implements OnInit{
constructor(){ }
ngOnInit(){ }
}
Emulated: From the above given three modes of view encapsulation, Emulated is the default and we don’t need to add this.
Native: Besides None, we can also choose Native and Native uses the shadow dom technology. This would give you the same result as before with Emulated but only in the browsers which supported shadow dom which is why in most cases we will use Emulated but we are aware that we could switch to Native or None as per our requirements.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Satwinder Singh
Satwinder had been working as a free-lancer for past 1-year and recently joined Oodles Technologies as a UI-Developer. His skills are : Jquery , Html , Css , Bootstrap and Javascript.