#6: πŸ“₯ Property binding

We now have our input-button-unit component, but it does not do much. We want to bring it to life.

Let's add an HTML input element and make its control text reflect the value of the title property.

We'll revert the component to its state before our experiments with its methods:

src/app/input-button-unit/input-button-unit.component.ts
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-input-button-unit',
  standalone: true,
  imports: [CommonModule],
  template: `
    <p>
      input-button-unit works!
      The title is: {{ title }}
    </p>
  `,  
  styleUrl: './input-button-unit.component.scss'
})    
export class InputButtonUnitComponent {
  title = 'Hello World';           

  constructor() { }                     
}

Let's add an input element and a button to the template:

src/app/input-button-unit/input-button-unit.component.ts
template: `
  <p>
    input-button-unit works!
    The title is: {{ title }}
  </p>

  <input>
  <button>Save</button>
`,

Reminder: We use interpolation to present the value of the title property: {{ title }}. Angular then presents the value of title each time that our app-input-button-unit component is shown.

What if we want to show the title value inside the HTML input control itself?

Every input element has an attribute called value, which holds the string that is displayed inside the input box. In HTML, we can pass a string directly to the element's value attribute:

src/app/input-button-unit/input-button-unit.component.ts
<input value="Hello World">

But we lose the dynamic binding between the properties in the controller and the template.

Angular lets us bind properties to the template easily and conveniently; we saw that with interpolation. Now we'll see how to bind to an element's property (not to be confused with class properties). We surround the wanted property with square brackets and pass it the class member:

src/app/input-button-unit/input-button-unit.component.ts
<input [value]="title">

Try this out and see the result in the browser!

a# Binding to Methods

The expressions that we can bind to in the template are not limited to class properties. They can be a method call or almost any other valid JavaScript expression.

src/app/input-button-unit/input-button-unit.component.ts
generateTitle(): string {
  return 'This title was generated by a method.';
}

Replace one or both of the bindings of the title in the template with the method call (don't forget the parentheses!):

src/app/input-button-unit/input-button-unit.component.ts
  <input [value]="generateTitle()">

  {{ generateTitle() }}

b# Change Detection

Angular has a very efficient change detection mechanism. It looks for bindings in the components' templates, and then updates the value each time the bound expression is changed.

src/app/input-button-unit/input-button-unit.component.ts
constructor() {
  setTimeout(() => {
    this.title = 'This is not the title you are looking for';
  }, 3000);
}

setTimeout is a JavaScript function. Its first parameter is what we want to happen - a function of our choice. The second parameter is how much we want to delay it, in milliseconds. In this example, we pass an inline anonymous function which sets the value of this.title. For this we use one of the new features in JavaScript ES6: an arrow function.

c# Binding to Methods

The expressions that we can bind to in the template are not limited to class properties. They can be a method call or almost any other valid Angular template expression.

d# Resources

Angular Guide - Template Property Binding

e# A note about accessing the DOM

Using regular JavaScript, we can insert the value to the input via its properties. We'll fetch the element from the DOM and assign the value of the member title to the element's value property.

code for example
let inputElement = document.getElementById('my-input');
inputElement.value = this.title;

In JavaScript, we find the input element in the DOM by its id, and then set its value property to the value of the title property. We need to add the id to the input element then:

code for example
<input id="my-input">

This will work in the browser.

However, this is highly discouraged in Angular. You should never access the DOM directly! That's because you can assign different renderers to Angular and run the application on different platforms. They may be renderers for mobile, desktop, or even a robot. These platforms will not have a document object from which you can manipulate the result!

πŸ’Ύ Save your code to GitHub

StackBlitz users - press Save in the toolbar and continue to the next section of the tutorial.

Commit all your changes by running this command in your project directory.

git add -A && git commit -m "Your Message"

Push your changes to GitHub by running this command in your project directory.

git push

Last updated