Professional Documents
Culture Documents
FAQ:
- What is Change Detection?
- What is Content Projection?
- Why to destroy a component?
Change Detection
- It is managed under “ngOnChanges()”.
- Sets the value to property.
- If no value defined into property then no change
detected.
- Binds the value to HTML element property.
- If there is a value defined into property and that is
bound to element property then Change detected.
- Detect the changes in value.
- Update the change to Model.
- “ngModel” with property and event binding [Two-way
binding] is one of the live examples of “Change
Detection”. [(ngModel)]
- Model is “Single-Source-of-Truth”.
- “SimpleChanges” is the base that identifies the changes
by accessing
o CurrentValue
o PreviousValue
- “SimpleChanges” object identified the changes in any
property and update the changes.
- SimpleChanges uses “SimpleChange” base class that
provides the properties
o previousValue:any
o currentValue:any
o firstChange:boolean
Ex:
- Add following components
o ng g c displayvalue
o ng g c sendvalue
- displayvalue.component.ts
import { Component, Input, OnChanges, SimpleChanges
} from '@angular/core';
@Component({
selector: 'app-displayvalue',
templateUrl: './displayvalue.component.html',
styleUrls: ['./displayvalue.component.css']
})
export class DisplayvalueComponent implements
OnChanges {
ngOnChanges(changes: SimpleChanges){
for(var property in changes) {
let change = changes[property];
this.currentValue = change.currentValue;
this.previousValue = change.previousValue;
}
if(this.currentValue==this.previousValue){
this.msg = 'No Change Detected';
} else {
this.msg = 'Change Detected';
}
}
}
- Displayvalue.component.html
<div>
<h2>Child Component</h2>
Hello ! {{userName}}
<h2>{{msg}}</h2>
<dl>
<dt>Previous Value</dt>
<dd>{{previousValue}}</dd>
<dt>Current Value</dt>
<dd>{{currentValue}}</dd>
</dl>
</div>
- Sendvalue.component.ts
export class SendvalueComponent {
public userName;
}
- Sendvalue.component.html
<div class="container-fluid">
<h2>Parent Component</h2>
<div>
<label>User Name</label>
<div>
<input [(ngModel)]="userName" type="text">
</div>
</div>
<div>
<app-displayvalue [userName]="userName"></app-
displayvalue>
</div>
</div>
Content Projection
- It is a way to import HTML content from outside the
component and insert that content into component
template at specific location.
- ngAfterContentChecked() is the life cycle hook
responsible for content projection.
- The external templates are access and display at
any specific location by using an object of type
“TemplateRef”.
- A “TemplateRef” object is responsible to
rendering external template into component at
specific location.
- TemplateRef and handle multiple content.
- Which content to display in a view is explicitly
managed by using another decorator
“@ViewChild()”
- It can display one content before change and
another content after change.
@ViewChild
- It is a property decorator.
- It configures a query in view.
- It is responsible for content projection.
- It configures a query based on selector and select
a view to render into the component at specific
location.
- It can project the content explicitly.
- It has two situations to render the content
o Before Change Detection
o After Change Detection
- You can configure the meta data for
“@ViewChild()” by using following properties:
o Selector: It specifies the template name to
render explicitly.
o Read: It is used to read a token explicitly and
render value from token.
o Static: If set to “true” then content will be
projected before change detection. If set to
false then content will be projected after
change detection. Default is false.
@Component({
selector: 'app-contentprojection',
templateUrl: './contentprojection.component.html',
styleUrls: ['./contentprojection.component.css']
})
export class ContentprojectionComponent implements
OnInit {
constructor() { }
ngOnInit(): void {
this.thenBlock = this.preview;
}
public Toggle() {
this.isVisible = true;
this.thenBlock = this.thenBlock==this.preview?
this.details: this.preview;
}
}
Contentprojection.component.html
<div class="container-fluid">
<h2>Product Details</h2>
<div class="card">
<div class="card-header">
<button (click)="Toggle()" class="btn btn-primary
btn-block">Details / Preview</button>
</div>
<div class="card-body">
<div *ngIf="isVisible; then thenBlock else elseBlock
"></div>
<ng-template #details>
<table class="table table-hover">
<tr>
<td>Name</td>
<td>Nike Casuals</td>
</tr>
<tr>
<td>Price</td>
<td>5600.53</td>
</tr>
<tr>
<td>Code</td>
<td>#0101NIKE</td>
</tr>
</table>
</ng-template>
<ng-template #preview>
<img src="assets/shoe.jpg" width="200"
height="200">
</ng-template>
<ng-template #elseBlock>
<h2>Click Preview / Details</h2>
</ng-template>
</div>
</div>
</div>
Angular Pipe, Services, Provides