Access Child Components From the Template

In our templates, we may find ourselves needing to access values provided by the child components which we use to build our own component.

The most straightforward examples of this may be seen dealing with forms or inputs:

app/my-example.component.html

<section >
  <form #myForm="ngForm" (ngSubmit)="submitForm(myForm)">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" ngModel>
    <button type="submit">Submit</button>
  </form>
</section>

app/my-example.component.ts

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

@Component({
  selector: 'my-example',
  templateUrl: 'app/my-example.component.html'
})
export class MyExampleComponent {
  submitForm (form: NgForm) {
    console.log(form.value);
  }
}

View Example

This isn't a magic feature which only forms or inputs have, but rather a way of referencing the instance of a child component in your template. With that reference, you can then access public properties and methods on that component.

<red-ball #myBall></red-ball>
The ball is {{ myBall.color }}.
@Component({
  selector: 'red-ball',
  template: '<div > · </div>'
})
export class RedBallComponent {
  color: string = 'red'
}

View Example

There are other means of accessing and interfacing with child components, but if you simply need to reference properties or methods of a child, this can be a simple and straightforward method of doing so.

results matching ""

    No results matching ""