-1

Using a new angular project:

Consider the following app.component.ts (notice the redSelected boolean)

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  redSelected = false;

}

It has the following app.component.html

<h2>Choose:</h2>
<label>
  <input type="radio" name="color" value="red" (change)="redSelected = true">
  <span>Red</span>
</label>
<label>
  <input type="radio" name="color" value="blue" (change)="redSelected = false">
  <span>Blue</span>
</label>
<p *ngIf="redSelected">Whoooorraay! Red is here.</p>

Whenever the Red radiobox is selected, the final paragraph appears. When the Blue radiobox is selected the paragraph disappears.

How is it possible that angular detects this change and updates the component html view?

Note: I tried replacing the final paragraph with

<p [ngClass]="hideOrShow()">Whoooorraay! Red is here.</p>

where hideOrShow() is defined (inside app.component.ts) as the following:

hideOrShow() {
  if (this.redSelected === false) return 'hidden';
}

and hidden is a css class (inside app.component.css)

.hidden {
    display: none;
}

The same behavior continues... Angular is able to detect the change in the value of redSelected and updates the component view html automatically.

My question is: what triggers this re-evaluation? what is being re-evaluated exactly?

Thanks in advance!

Maged
  • 71
  • 1
  • 8
  • 1
    Have you read e.g. https://stackoverflow.com/q/35469024/3001761? – jonrsharpe Feb 17 '20 at 01:41
  • @jonrsharpe Thanks a lot! I have been trying to find documentation on this but without knowing the correct terminology. If there is any official docs on the topic, kindly link it here. Thanks again, – Maged Feb 17 '20 at 01:45

1 Answers1

0

Each angular component has attached change detector that test changes in templates variables. You can read more about it here

Xesenix
  • 2,418
  • 15
  • 30