Angular Part2
Angular Part2
Three types:
Viewencapsulation.none
Viewencapsulation.emulated
Viewencapsulation.shadowDom
Viewencapsulation.emulated:
The unique attribute id is used.
Which helps in achieving view encapsulation the parent and
child components of same tag are separated by unique id.
Viewencapsulation.None:
In component by assigning encapsulation
=ViewEncapsulation.None
There be no unique id hence if we apply style to parent
component it also reflected in child component.
Viewencapsulation.ShadowDom:
First makes the particular style in parent to global in style.css
then It get applied to all components
But in particular component declaring encapsulation
=Viewencapsulation.ShadowDom then it makes the particular
component to be separate from the other Dom. And it get
affected to the style from global style.
ng-content :
which is used for making the content to be dynamically added to
it.
Used for code reusability.
<!-- app-parent.component.html
<div>
<app-child>
<p>this is 1</p>
</app-child>
<app-child>t
<p>this is 2</p>
</app-child>
<app-child>
<p>this is 3</p>
</app-child>
</div>
Hooks:
These all called order wise.
First Constructor called it is not a hook.
1. ngOnChange - whenever input bound property @input of
component or directive changes.
app.component.html
<div>
<app-child>
<p #para>this is old para</p>
</app-child>
</div>
app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { ChildComponent } from './child/child.component';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet,ChildComponent],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'contentchilddecorator';
}
child.component.html
child.component.ts
@Component({
selector: 'app-child',
standalone: true,
imports: [CommonModule],
templateUrl: './child.component.html',
styleUrl: './child.component.css'
})
export class ChildComponent {
@ContentChild('para')
paragraph!: ElementRef;
call()
{
this.paragraph.nativeElement.textContent="this is new para";
}
}
CUSTOM ATTRIBUTE DIRECTIVE:
Creating attribute customly like built in directives ngStyle, ngClass.
<div>
<app-child>
<p #para setbackground>this is old para</p>
</app-child>
</div>
Renderer2 :