Welcome to Knowledge Base!

KB at your finger tips

Book a Meeting to Avail the Services of Angular overtime

This is one stop global knowledge base where you can learn about all the products, solutions and support features.

Categories
All

Angular

Angular - Complex animation sequences

Complex animation sequences link

Prerequisites link

A basic understanding of the following concepts:

  • Introduction to Angular animations
  • Transition and triggers

So far, we've learned simple animations of single HTML elements. Angular also lets you animate coordinated sequences, such as an entire grid or list of elements as they enter and leave a page. You can choose to run multiple animations in parallel, or run discrete animations sequentially, one following another.

The functions that control complex animation sequences are:

Functions Details
query() Finds one or more inner HTML elements.
stagger() Applies a cascading delay to animations for multiple elements.
group() Runs multiple animation steps in parallel.
sequence() Runs animation steps one after another.

The query() function link

Most complex animations rely on the query() function to find child elements and apply animations to them, basic examples of such are:

Examples Details
query() followed by animate() Used to query simple HTML elements and directly apply animations to them.
query() followed by animateChild() Used to query child elements, which themselves have animations metadata applied to them and trigger such animation (which would be otherwise be blocked by the current/parent element's animation).

The first argument of query() is a css selector string which can also contain the following Angular-specific tokens:

Tokens Details
:enter
:leave
For entering/leaving elements.
:animating For elements currently animating.
@*
@triggerName
For elements with any—or a specific—trigger.
:self The animating element itself.
Entering and Leaving Elements

Not all child elements are actually considered as entering/leaving; this can, at times, be counterintuitive and confusing. Please see the query api docs for more information.

You can also see an illustration of this in the animations live example (introduced in the animations introduction section) under the Querying tab.

Animate multiple elements using query() and stagger() functions link

After having queried child elements via query() , the stagger() function lets you define a timing gap between each queried item that is animated and thus animates elements with a delay between them.

The following example demonstrates how to use the query() and stagger() functions to animate a list (of heroes) adding each in sequence, with a slight delay, from top to bottom.

  • Use query() to look for an element entering the page that meets certain criteria

  • For each of these elements, use style() to set the same initial style for the element. Make it transparent and use transform to move it out of position so that it can slide into place.

  • Use stagger() to delay each animation by 30 milliseconds

  • Animate each element on screen for 0.5 seconds using a custom-defined easing curve, simultaneously fading it in and un-transforming it

src/app/hero-list-page.component.ts
      
      animations: [
  trigger('pageAnimations', [
    transition(':enter', [
      query('.hero', [
        style({opacity: 0, transform: 'translateY(-100px)'}),
        stagger(30, [
          animate('500ms cubic-bezier(0.35, 0, 0.25, 1)',
          style({ opacity: 1, transform: 'none' }))
        ])
      ])
    ])
  ]),
    

Parallel animation using group() function link

You've seen how to add a delay between each successive animation. But you might also want to configure animations that happen in parallel. For example, you might want to animate two CSS properties of the same element but use a different easing function for each one. For this, you can use the animation group() function.

NOTE :
The group() function is used to group animation steps , rather than animated elements.

The following example uses group() s on both :enter and :leave for two different timing configurations, thus applying two independent animations to the same element in parallel.

src/app/hero-list-groups.component.ts (excerpt)
      
      animations: [
  trigger('flyInOut', [
    state('in', style({
      width: '*',
      transform: 'translateX(0)', opacity: 1
    })),
    transition(':enter', [
      style({ width: 10, transform: 'translateX(50px)', opacity: 0 }),
      group([
        animate('0.3s 0.1s ease', style({
          transform: 'translateX(0)',
          width: '*'
        })),
        animate('0.3s ease', style({
          opacity: 1
        }))
      ])
    ]),
    transition(':leave', [
      group([
        animate('0.3s ease', style({
          transform: 'translateX(50px)',
          width: 10
        })),
        animate('0.3s 0.2s ease', style({
          opacity: 0
        }))
      ])
    ])
  ])
]
    

Sequential vs. parallel animations link

Complex animations can have many things happening at once. But what if you want to create an animation involving several animations happening one after the other? Earlier you used group() to run multiple animations all at the same time, in parallel.

A second function called sequence() lets you run those same animations one after the other. Within sequence() , the animation steps consist of either style() or animate() function calls.

  • Use style() to apply the provided styling data immediately.
  • Use animate() to apply styling data over a given time interval.

Filter animation example link

Take a look at another animation on the live example page. Under the Filter/Stagger tab, enter some text into the Search Heroes text box, such as Magnet or tornado .

The filter works in real time as you type. Elements leave the page as you type each new letter and the filter gets progressively stricter. The heroes list gradually re-enters the page as you delete each letter in the filter box.

The HTML template contains a trigger called filterAnimation .

src/app/hero-list-page.component.html
      
      <label for="search">Search heroes: </label>
<input type="text" id="search" #criteria
       (input)="updateCriteria(criteria.value)"
       placeholder="Search heroes">

<ul class="heroes" [@filterAnimation]="heroesTotal">
  <li *ngFor="let hero of heroes" class="hero">
    <div class="inner">
      <span class="badge">{{ hero.id }}</span>
      <span class="name">{{ hero.name }}</span>
    </div>
  </li>
</ul>
    

The filterAnimation in the component's decorator contains three transitions.

src/app/hero-list-page.component.ts
      
      @Component({
  animations: [
    trigger('filterAnimation', [
      transition(':enter, * => 0, * => -1', []),
      transition(':increment', [
        query(':enter', [
          style({ opacity: 0, width: 0 }),
          stagger(50, [
            animate('300ms ease-out', style({ opacity: 1, width: '*' })),
          ]),
        ], { optional: true })
      ]),
      transition(':decrement', [
        query(':leave', [
          stagger(50, [
            animate('300ms ease-out', style({ opacity: 0, width: 0 })),
          ]),
        ])
      ]),
    ]),
  ]
})
export class HeroListPageComponent implements OnInit {
  heroesTotal = -1;

  get heroes() { return this._heroes; }
  private _heroes: Hero[] = [];

  ngOnInit() {
    this._heroes = HEROES;
  }

  updateCriteria(criteria: string) {
    criteria = criteria ? criteria.trim() : '';

    this._heroes = HEROES.filter(hero => hero.name.toLowerCase().includes(criteria.toLowerCase()));
    const newTotal = this.heroes.length;

    if (this.heroesTotal !== newTotal) {
      this.heroesTotal = newTotal;
    } else if (!criteria) {
      this.heroesTotal = -1;
    }
  }
}
    

The code in this example performs the following tasks:

  • Skips animations when the user first opens or navigates to this page (the filter animation narrows what is already there, so it only works on elements that already exist in the DOM)
  • Filters heroes based on the search input's value

For each change:

  • Hides an element leaving the DOM by setting its opacity and width to 0

  • Animates an element entering the DOM over 300 milliseconds. During the animation, the element assumes its default width and opacity.

  • If there are multiple elements entering or leaving the DOM, staggers each animation starting at the top of the page, with a 50-millisecond delay between each element

Animating the items of a reordering list link

Although Angular animates correctly *ngFor list items out of the box, it will not be able to do so if their ordering changes. This is because it will lose track of which element is which, resulting in broken animations. The only way to help Angular keep track of such elements is by assigning a TrackByFunction to the NgForOf directive. This makes sure that Angular always knows which element is which, thus allowing it to apply the correct animations to the correct elements all the time.

IMPORTANT :
If you need to animate the items of an *ngFor list and there is a possibility that the order of such items will change during runtime, always use a TrackByFunction .

Animations and Component View Encapsulation link

Angular animations are based on the components DOM structure and do not directly take View Encapsulation into account, this means that components using ViewEncapsulation.Emulated behave exactly as if they were using ViewEncapsulation.None ( ViewEncapsulation.ShadowDom behaves differently as we'll discuss shortly).

For example if the query() function (which you'll see more of in the rest of the Animations guide) were to be applied at the top of a tree of components using the emulated view encapsulation, such query would be able to identify (and thus animate) DOM elements on any depth of the tree.

On the other hand the ViewEncapsulation.ShadowDom changes the component's DOM structure by "hiding" DOM elements inside ShadowRoot elements. Such DOM manipulations do prevent some of the animations implementation to work properly since it relies on simple DOM structures and doesn't take ShadowRoot elements into account. Therefore it is advised to avoid applying animations to views incorporating components using the ShadowDom view encapsulation.

Animation sequence summary link

Angular functions for animating multiple elements start with query() to find inner elements; for example, gathering all images within a <div> . The remaining functions, stagger() , group() , and sequence() , apply cascades or let you control how multiple animation steps are applied.

More on Angular animations link

You might also be interested in the following:

  • Introduction to Angular animations
  • Transition and triggers
  • Reusable animations
  • Route transition animations
Last reviewed on Mon Feb 28 2022

Stay Ahead in Today’s Competitive Market!
Unlock your company’s full potential with a Virtual Delivery Center (VDC). Gain specialized expertise, drive seamless operations, and scale effortlessly for long-term success.

Book a Meeting to Avail the Services of Angularovertime

Angular

link

Last reviewed on Mon Aug 22 2022
Read article

Angular

link

Last reviewed on Mon Aug 22 2022
Read article

Angular

link

Last reviewed on Mon Aug 22 2022
Read article

Angular

link

Last reviewed on Mon Aug 22 2022
Read article

Angular - Component interaction

Component interaction link

This cookbook contains recipes for common component communication scenarios in which two or more components share information.

See the live example / download example .

Pass data from parent to child with input binding link

HeroChildComponent has two input properties , typically adorned with @Input() decorator.

component-interaction/src/app/hero-child.component.ts
      
      import { Component, Input } from '@angular/core';

import { Hero } from './hero';

@Component({
  selector: 'app-hero-child',
  template: `
    <h3>{{hero.name}} says:</h3>
    <p>I, {{hero.name}}, am at your service, {{masterName}}.</p>
  `
})
export class HeroChildComponent {
  @Input() hero!: Hero;
  @Input('master') masterName = '';
}
    

The second @Input aliases the child component property name masterName as 'master' .

The HeroParentComponent nests the child HeroChildComponent inside an *ngFor repeater, binding its master string property to the child's master alias, and each iteration's hero instance to the child's hero property.

component-interaction/src/app/hero-parent.component.ts
      
      import { Component } from '@angular/core';

import { HEROES } from './hero';

@Component({
  selector: 'app-hero-parent',
  template: `
    <h2>{{master}} controls {{heroes.length}} heroes</h2>

    <app-hero-child
      *ngFor="let hero of heroes"
      [hero]="hero"
      [master]="master">
    </app-hero-child>
  `
})
export class HeroParentComponent {
  heroes = HEROES;
  master = 'Master';
}
    

The running application displays three heroes:

Test it for Pass data from parent to child with input binding link

E2E test that all children were instantiated and displayed as expected:

component-interaction/e2e/src/app.e2e-spec.ts
      
      // ...
const heroNames = ['Dr. IQ', 'Magneta', 'Bombasto'];
const masterName = 'Master';

it('should pass properties to children properly', async () => {
  const parent = element(by.tagName('app-hero-parent'));
  const heroes = parent.all(by.tagName('app-hero-child'));

  for (let i = 0; i < heroNames.length; i++) {
    const childTitle = await heroes.get(i).element(by.tagName('h3')).getText();
    const childDetail = await heroes.get(i).element(by.tagName('p')).getText();
    expect(childTitle).toEqual(heroNames[i] + ' says:');
    expect(childDetail).toContain(masterName);
  }
});
// ...
    

Back to top

Intercept input property changes with a setter link

Use an input property setter to intercept and act upon a value from the parent.

The setter of the name input property in the child NameChildComponent trims the whitespace from a name and replaces an empty value with default text.

component-interaction/src/app/name-child.component.ts
      
      import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-name-child',
  template: '<h3>"{{name}}"</h3>'
})
export class NameChildComponent {
  @Input()
  get name(): string { return this._name; }
  set name(name: string) {
    this._name = (name && name.trim()) || '<no name set>';
  }
  private _name = '';
}
    

Here's the NameParentComponent demonstrating name variations including a name with all spaces:

component-interaction/src/app/name-parent.component.ts
      
      import { Component } from '@angular/core';

@Component({
  selector: 'app-name-parent',
  template: `
    <h2>Master controls {{names.length}} names</h2>

    <app-name-child *ngFor="let name of names" [name]="name"></app-name-child>
  `
})
export class NameParentComponent {
  // Displays 'Dr. IQ', '<no name set>', 'Bombasto'
  names = ['Dr. IQ', '   ', '  Bombasto  '];
}
    

Test it for Intercept input property changes with a setter link

E2E tests of input property setter with empty and non-empty names:

component-interaction/e2e/src/app.e2e-spec.ts
      
      // ...
it('should display trimmed, non-empty names', async () => {
  const nonEmptyNameIndex = 0;
  const nonEmptyName = '"Dr. IQ"';
  const parent = element(by.tagName('app-name-parent'));
  const hero = parent.all(by.tagName('app-name-child')).get(nonEmptyNameIndex);

  const displayName = await hero.element(by.tagName('h3')).getText();
  expect(displayName).toEqual(nonEmptyName);
});

it('should replace empty name with default name', async () => {
  const emptyNameIndex = 1;
  const defaultName = '"<no name set>"';
  const parent = element(by.tagName('app-name-parent'));
  const hero = parent.all(by.tagName('app-name-child')).get(emptyNameIndex);

  const displayName = await hero.element(by.tagName('h3')).getText();
  expect(displayName).toEqual(defaultName);
});
// ...
    

Back to top

Intercept input property changes with ngOnChanges() link

Detect and act upon changes to input property values with the ngOnChanges() method of the OnChanges lifecycle hook interface.

You might prefer this approach to the property setter when watching multiple, interacting input properties.

Learn about ngOnChanges() in the Lifecycle Hooks chapter.

This VersionChildComponent detects changes to the major and minor input properties and composes a log message reporting these changes:

component-interaction/src/app/version-child.component.ts
      
      import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-version-child',
  template: `
    <h3>Version {{major}}.{{minor}}</h3>
    <h4>Change log:</h4>
    <ul>
      <li *ngFor="let change of changeLog">{{change}}</li>
    </ul>
  `
})
export class VersionChildComponent implements OnChanges {
  @Input() major = 0;
  @Input() minor = 0;
  changeLog: string[] = [];

  ngOnChanges(changes: SimpleChanges) {
    const log: string[] = [];
    for (const propName in changes) {
      const changedProp = changes[propName];
      const to = JSON.stringify(changedProp.currentValue);
      if (changedProp.isFirstChange()) {
        log.push(`Initial value of ${propName} set to ${to}`);
      } else {
        const from = JSON.stringify(changedProp.previousValue);
        log.push(`${propName} changed from ${from} to ${to}`);
      }
    }
    this.changeLog.push(log.join(', '));
  }
}
    

The VersionParentComponent supplies the minor and major values and binds buttons to methods that change them.

component-interaction/src/app/version-parent.component.ts
      
      import { Component } from '@angular/core';

@Component({
  selector: 'app-version-parent',
  template: `
    <h2>Source code version</h2>
    <button type="button" (click)="newMinor()">New minor version</button>
    <button type="button" (click)="newMajor()">New major version</button>
    <app-version-child [major]="major" [minor]="minor"></app-version-child>
  `
})
export class VersionParentComponent {
  major = 1;
  minor = 23;

  newMinor() {
    this.minor++;
  }

  newMajor() {
    this.major++;
    this.minor = 0;
  }
}
    

Here's the output of a button-pushing sequence:

Test it for Intercept input property changes with ngOnChanges() link

Test that both input properties are set initially and that button clicks trigger the expected ngOnChanges calls and values:

component-interaction/e2e/src/app.e2e-spec.ts
      
      // ...
// Test must all execute in this exact order
it('should set expected initial values', async () => {
  const actual = await getActual();

  const initialLabel = 'Version 1.23';
  const initialLog = 'Initial value of major set to 1, Initial value of minor set to 23';

  expect(actual.label).toBe(initialLabel);
  expect(actual.count).toBe(1);
  expect(await actual.logs.get(0).getText()).toBe(initialLog);
});

it("should set expected values after clicking 'Minor' twice", async () => {
  const repoTag = element(by.tagName('app-version-parent'));
  const newMinorButton = repoTag.all(by.tagName('button')).get(0);

  await newMinorButton.click();
  await newMinorButton.click();

  const actual = await getActual();

  const labelAfter2Minor = 'Version 1.25';
  const logAfter2Minor = 'minor changed from 24 to 25';

  expect(actual.label).toBe(labelAfter2Minor);
  expect(actual.count).toBe(3);
  expect(await actual.logs.get(2).getText()).toBe(logAfter2Minor);
});

it("should set expected values after clicking 'Major' once", async () => {
  const repoTag = element(by.tagName('app-version-parent'));
  const newMajorButton = repoTag.all(by.tagName('button')).get(1);

  await newMajorButton.click();
  const actual = await getActual();

  const labelAfterMajor = 'Version 2.0';
  const logAfterMajor = 'major changed from 1 to 2, minor changed from 23 to 0';

  expect(actual.label).toBe(labelAfterMajor);
  expect(actual.count).toBe(2);
  expect(await actual.logs.get(1).getText()).toBe(logAfterMajor);
});

async function getActual() {
  const versionTag = element(by.tagName('app-version-child'));
  const label = await versionTag.element(by.tagName('h3')).getText();
  const ul = versionTag.element((by.tagName('ul')));
  const logs = ul.all(by.tagName('li'));

  return {
    label,
    logs,
    count: await logs.count(),
  };
}
// ...
    

Back to top

Parent listens for child event link

The child component exposes an EventEmitter property with which it emits events when something happens. The parent binds to that event property and reacts to those events.

The child's EventEmitter property is an output property , typically adorned with an @Output() decorator as seen in this VoterComponent :

component-interaction/src/app/voter.component.ts
      
      import { Component, EventEmitter, Input, Output } from '@angular/core';

@Component({
  selector: 'app-voter',
  template: `
    <h4>{{name}}</h4>
    <button type="button" (click)="vote(true)"  [disabled]="didVote">Agree</button>
    <button type="button" (click)="vote(false)" [disabled]="didVote">Disagree</button>
  `
})
export class VoterComponent {
  @Input()  name = '';
  @Output() voted = new EventEmitter<boolean>();
  didVote = false;

  vote(agreed: boolean) {
    this.voted.emit(agreed);
    this.didVote = true;
  }
}
    

Clicking a button triggers emission of a true or false , the boolean payload .

The parent VoteTakerComponent binds an event handler called onVoted() that responds to the child event payload $event and updates a counter.

component-interaction/src/app/votetaker.component.ts
      
      import { Component } from '@angular/core';

@Component({
  selector: 'app-vote-taker',
  template: `
    <h2>Should mankind colonize the Universe?</h2>
    <h3>Agree: {{agreed}}, Disagree: {{disagreed}}</h3>

    <app-voter
      *ngFor="let voter of voters"
      [name]="voter"
      (voted)="onVoted($event)">
    </app-voter>
  `
})
export class VoteTakerComponent {
  agreed = 0;
  disagreed = 0;
  voters = ['Dr. IQ', 'Celeritas', 'Bombasto'];

  onVoted(agreed: boolean) {
    if (agreed) {
      this.agreed++;
    } else {
      this.disagreed++;
    }
  }
}
    

The framework passes the event argument —represented by $event — to the handler method, and the method processes it:

Test it for Parent listens for child event link

Test that clicking the Agree and Disagree buttons update the appropriate counters:

component-interaction/e2e/src/app.e2e-spec.ts
      
      // ...
it('should not emit the event initially', async () => {
  const voteLabel = element(by.tagName('app-vote-taker')).element(by.tagName('h3'));
  expect(await voteLabel.getText()).toBe('Agree: 0, Disagree: 0');
});

it('should process Agree vote', async () => {
  const voteLabel = element(by.tagName('app-vote-taker')).element(by.tagName('h3'));
  const agreeButton1 = element.all(by.tagName('app-voter')).get(0)
    .all(by.tagName('button')).get(0);

  await agreeButton1.click();

  expect(await voteLabel.getText()).toBe('Agree: 1, Disagree: 0');
});

it('should process Disagree vote', async () => {
  const voteLabel = element(by.tagName('app-vote-taker')).element(by.tagName('h3'));
  const agreeButton1 = element.all(by.tagName('app-voter')).get(1)
    .all(by.tagName('button')).get(1);

  await agreeButton1.click();

  expect(await voteLabel.getText()).toBe('Agree: 0, Disagree: 1');
});
// ...
    

Back to top

Parent interacts with child using local variable link

A parent component cannot use data binding to read child properties or invoke child methods. Do both by creating a template reference variable for the child element and then reference that variable within the parent template as seen in the following example.

The following is a child CountdownTimerComponent that repeatedly counts down to zero and launches a rocket. The start and stop methods control the clock and a countdown status message displays in its own template.

component-interaction/src/app/countdown-timer.component.ts
      
      import { Component, OnDestroy } from '@angular/core';

@Component({
  selector: 'app-countdown-timer',
  template: '<p>{{message}}</p>'
})
export class CountdownTimerComponent implements OnDestroy {

  intervalId = 0;
  message = '';
  seconds = 11;

  ngOnDestroy() { this.clearTimer(); }

  start() { this.countDown(); }
  stop()  {
    this.clearTimer();
    this.message = `Holding at T-${this.seconds} seconds`;
  }

  private clearTimer() { clearInterval(this.intervalId); }

  private countDown() {
    this.clearTimer();
    this.intervalId = window.setInterval(() => {
      this.seconds -= 1;
      if (this.seconds === 0) {
        this.message = 'Blast off!';
      } else {
        if (this.seconds < 0) { this.seconds = 10; } // reset
        this.message = `T-${this.seconds} seconds and counting`;
      }
    }, 1000);
  }
}
    

The CountdownLocalVarParentComponent that hosts the timer component is as follows:

component-interaction/src/app/countdown-parent.component.ts
      
      import { Component } from '@angular/core';
import { CountdownTimerComponent } from './countdown-timer.component';

@Component({
  selector: 'app-countdown-parent-lv',
  template: `
    <h3>Countdown to Liftoff (via local variable)</h3>
    <button type="button" (click)="timer.start()">Start</button>
    <button type="button" (click)="timer.stop()">Stop</button>
    <div class="seconds">{{timer.seconds}}</div>
    <app-countdown-timer #timer></app-countdown-timer>
  `,
  styleUrls: ['../assets/demo.css']
})
export class CountdownLocalVarParentComponent { }
    

The parent component cannot data bind to the child's start and stop methods nor to its seconds property.

Place a local variable, #timer , on the tag <app-countdown-timer> representing the child component. That gives you a reference to the child component and the ability to access any of its properties or methods from within the parent template.

This example wires parent buttons to the child's start and stop and uses interpolation to display the child's seconds property.

Here, the parent and child are working together.

Test it for Parent interacts with child using local variable link

Test that the seconds displayed in the parent template match the seconds displayed in the child's status message. Test also that clicking the Stop button pauses the countdown timer:

component-interaction/e2e/src/app.e2e-spec.ts
      
      // ...
// The tests trigger periodic asynchronous operations (via `setInterval()`), which will prevent
// the app from stabilizing. See https://angular.io/api/core/ApplicationRef#is-stable-examples
// for more details.
// To allow the tests to complete, we will disable automatically waiting for the Angular app to
// stabilize.
beforeEach(() => browser.waitForAngularEnabled(false));
afterEach(() => browser.waitForAngularEnabled(true));

it('timer and parent seconds should match', async () => {
  const parent = element(by.tagName(parentTag));
  const startButton = parent.element(by.buttonText('Start'));
  const seconds = parent.element(by.className('seconds'));
  const timer = parent.element(by.tagName('app-countdown-timer'));

  await startButton.click();

  // Wait for `<app-countdown-timer>` to be populated with any text.
  await browser.wait(() => timer.getText(), 2000);

  expect(await timer.getText()).toContain(await seconds.getText());
});

it('should stop the countdown', async () => {
  const parent = element(by.tagName(parentTag));
  const startButton = parent.element(by.buttonText('Start'));
  const stopButton = parent.element(by.buttonText('Stop'));
  const timer = parent.element(by.tagName('app-countdown-timer'));

  await startButton.click();
  expect(await timer.getText()).not.toContain('Holding');

  await stopButton.click();
  expect(await timer.getText()).toContain('Holding');
});
// ...
    

Back to top

Parent calls an @ViewChild() link

The local variable approach is straightforward. But it is limited because the parent-child wiring must be done entirely within the parent template. The parent component itself has no access to the child.

You can't use the local variable technique if the parent component's class relies on the child component's class . The parent-child relationship of the components is not established within each component's respective class with the local variable technique. Because the class instances are not connected to one another, the parent class cannot access the child class properties and methods.

When the parent component class requires that kind of access, inject the child component into the parent as a ViewChild .

The following example illustrates this technique with the same Countdown Timer example. Neither its appearance nor its behavior changes. The child CountdownTimerComponent is the same as well.

The switch from the local variable to the ViewChild technique is solely for the purpose of demonstration.

Here is the parent, CountdownViewChildParentComponent :

component-interaction/src/app/countdown-parent.component.ts
      
      import { AfterViewInit, ViewChild } from '@angular/core';
import { Component } from '@angular/core';
import { CountdownTimerComponent } from './countdown-timer.component';

@Component({
  selector: 'app-countdown-parent-vc',
  template: `
    <h3>Countdown to Liftoff (via ViewChild)</h3>
    <button type="button" (click)="start()">Start</button>
    <button type="button" (click)="stop()">Stop</button>
    <div class="seconds">{{ seconds() }}</div>
    <app-countdown-timer></app-countdown-timer>
  `,
  styleUrls: ['../assets/demo.css']
})
export class CountdownViewChildParentComponent implements AfterViewInit {

  @ViewChild(CountdownTimerComponent)
  private timerComponent!: CountdownTimerComponent;

  seconds() { return 0; }

  ngAfterViewInit() {
    // Redefine `seconds()` to get from the `CountdownTimerComponent.seconds` ...
    // but wait a tick first to avoid one-time devMode
    // unidirectional-data-flow-violation error
    setTimeout(() => this.seconds = () => this.timerComponent.seconds, 0);
  }

  start() { this.timerComponent.start(); }
  stop() { this.timerComponent.stop(); }
}
    

It takes a bit more work to get the child view into the parent component class .

First, you have to import references to the ViewChild decorator and the AfterViewInit lifecycle hook.

Next, inject the child CountdownTimerComponent into the private timerComponent property using the @ViewChild property decoration.

The #timer local variable is gone from the component metadata. Instead, bind the buttons to the parent component's own start and stop methods and present the ticking seconds in an interpolation around the parent component's seconds method.

These methods access the injected timer component directly.

The ngAfterViewInit() lifecycle hook is an important wrinkle. The timer component isn't available until after Angular displays the parent view. So it displays 0 seconds initially.

Then Angular calls the ngAfterViewInit lifecycle hook at which time it is too late to update the parent view's display of the countdown seconds. Angular's unidirectional data flow rule prevents updating the parent view's in the same cycle. The application must wait one turn before it can display the seconds.

Use setTimeout() to wait one tick and then revise the seconds() method so that it takes future values from the timer component.

Test it for Parent calls an @ViewChild() link

Use the same countdown timer tests as before.

Back to top

Parent and children communicate using a service link

A parent component and its children share a service whose interface enables bidirectional communication within the family .

The scope of the service instance is the parent component and its children. Components outside this component subtree have no access to the service or their communications.

This MissionService connects the MissionControlComponent to multiple AstronautComponent children.

component-interaction/src/app/mission.service.ts
      
      import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable()
export class MissionService {

  // Observable string sources
  private missionAnnouncedSource = new Subject<string>();
  private missionConfirmedSource = new Subject<string>();

  // Observable string streams
  missionAnnounced$ = this.missionAnnouncedSource.asObservable();
  missionConfirmed$ = this.missionConfirmedSource.asObservable();

  // Service message commands
  announceMission(mission: string) {
    this.missionAnnouncedSource.next(mission);
  }

  confirmMission(astronaut: string) {
    this.missionConfirmedSource.next(astronaut);
  }
}
    

The MissionControlComponent both provides the instance of the service that it shares with its children (through the providers metadata array) and injects that instance into itself through its constructor:

component-interaction/src/app/missioncontrol.component.ts
      
      import { Component } from '@angular/core';

import { MissionService } from './mission.service';

@Component({
  selector: 'app-mission-control',
  template: `
    <h2>Mission Control</h2>
    <button type="button" (click)="announce()">Announce mission</button>

    <app-astronaut
      *ngFor="let astronaut of astronauts"
      [astronaut]="astronaut">
    </app-astronaut>

    <h3>History</h3>
    <ul>
      <li *ngFor="let event of history">{{event}}</li>
    </ul>
  `,
  providers: [MissionService]
})
export class MissionControlComponent {
  astronauts = ['Lovell', 'Swigert', 'Haise'];
  history: string[] = [];
  missions = ['Fly to the moon!',
              'Fly to mars!',
              'Fly to Vegas!'];
  nextMission = 0;

  constructor(private missionService: MissionService) {
    missionService.missionConfirmed$.subscribe(
      astronaut => {
        this.history.push(`${astronaut} confirmed the mission`);
      });
  }

  announce() {
    const mission = this.missions[this.nextMission++];
    this.missionService.announceMission(mission);
    this.history.push(`Mission "${mission}" announced`);
    if (this.nextMission >= this.missions.length) { this.nextMission = 0; }
  }
}
    

The AstronautComponent also injects the service in its constructor. Each AstronautComponent is a child of the MissionControlComponent and therefore receives its parent's service instance:

component-interaction/src/app/astronaut.component.ts
      
      import { Component, Input, OnDestroy } from '@angular/core';

import { MissionService } from './mission.service';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-astronaut',
  template: `
    <p>
      {{astronaut}}: <strong>{{mission}}</strong>
      <button
        type="button"
        (click)="confirm()"
        [disabled]="!announced || confirmed">
        Confirm
      </button>
    </p>
  `
})
export class AstronautComponent implements OnDestroy {
  @Input() astronaut = '';
  mission = '<no mission announced>';
  confirmed = false;
  announced = false;
  subscription: Subscription;

  constructor(private missionService: MissionService) {
    this.subscription = missionService.missionAnnounced$.subscribe(
      mission => {
        this.mission = mission;
        this.announced = true;
        this.confirmed = false;
    });
  }

  confirm() {
    this.confirmed = true;
    this.missionService.confirmMission(this.astronaut);
  }

  ngOnDestroy() {
    // prevent memory leak when component destroyed
    this.subscription.unsubscribe();
  }
}
    

Notice that this example captures the subscription and unsubscribe() when the AstronautComponent is destroyed. This is a memory-leak guard step. There is no actual risk in this application because the lifetime of a AstronautComponent is the same as the lifetime of the application itself. That would not always be true in a more complex application.

You don't add this guard to the MissionControlComponent because, as the parent, it controls the lifetime of the MissionService .

The History log demonstrates that messages travel in both directions between the parent MissionControlComponent and the AstronautComponent children, facilitated by the service:

Test it for Parent and children communicate using a service link

Tests click buttons of both the parent MissionControlComponent and the AstronautComponent children and verify that the history meets expectations:

component-interaction/e2e/src/app.e2e-spec.ts
      
      // ...
it('should announce a mission', async () => {
  const missionControl = element(by.tagName('app-mission-control'));
  const announceButton = missionControl.all(by.tagName('button')).get(0);
  const history = missionControl.all(by.tagName('li'));

  await announceButton.click();

  expect(await history.count()).toBe(1);
  expect(await history.get(0).getText()).toMatch(/Mission.* announced/);
});

it('should confirm the mission by Lovell', async () => {
  await testConfirmMission(1, 'Lovell');
});

it('should confirm the mission by Haise', async () => {
  await testConfirmMission(3, 'Haise');
});

it('should confirm the mission by Swigert', async () => {
  await testConfirmMission(2, 'Swigert');
});

async function testConfirmMission(buttonIndex: number, astronaut: string) {
  const missionControl = element(by.tagName('app-mission-control'));
  const announceButton = missionControl.all(by.tagName('button')).get(0);
  const confirmButton = missionControl.all(by.tagName('button')).get(buttonIndex);
  const history = missionControl.all(by.tagName('li'));

  await announceButton.click();
  await confirmButton.click();

  expect(await history.count()).toBe(2);
  expect(await history.get(1).getText()).toBe(`${astronaut} confirmed the mission`);
}
// ...
    

Back to top

Last reviewed on Mon Feb 28 2022
Read article