Sub topic 2
Binding class properties with { }
This chapter assumes you can read basic HTML and modern JavaScript classes, and that you have created a Lightning Web Component bundle (HTML, JS, and meta XML files). It covers how a template reads values from its class.
The binding syntax
A binding connects a value in the component's JavaScript class to a place in the template. You write the property name inside curly braces. LWC looks the name up on the component instance and puts its value into the output.
Here is the template:
<!-- hello.html -->
<template>
<p>Hello, {greeting}!</p>
</template>Here is the class it reads from:
// hello.js
import { LightningElement } from 'lwc';
export default class Hello extends LightningElement {
greeting = 'World';
}The browser shows Hello, World!. The template and the class share the same name, greeting. If the template names a property that the class does not define, the value is undefined and renders as empty text.
What the braces accept
The text between the braces must be a property name, optionally followed by dotted access to nested values. These are valid:
{greeting}reads a field or getter on the class.{contact.label}reads a property of an object that the class holds.
Anything else is not valid. {quantity * unitPrice}, {items.length > 0}, and {name || 'guest'} all fail to compile. The next chapter explains why and what to use instead.
Binding text and attributes
Braces work in two places: as text content between tags, and as the value of an attribute.
<!-- link.html -->
<template>
<a href={url} title={tooltip}>{linkText}</a>
</template>// link.js
import { LightningElement } from 'lwc';
export default class Link extends LightningElement {
url = 'https://developer.salesforce.com';
tooltip = 'Salesforce developer site';
linkText = 'Open the docs';
}Do not put quotes around the braces in an attribute. Write href={url}, not href="{url}". The quoted form is a mistake that the compiler rejects, because the value would otherwise be ambiguous between a string and a binding.
How a value reaches the screen
The template reads the field when the component renders. When the field is reassigned later, the component renders again and the text updates. You never write code that finds the element and changes its text.
The binding is one way. Data moves from the class to the template. Changing text in the page does not change the class field. Later chapters show how events carry user input back.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Text is blank | The property name in the template does not match the class, including case | Match the name exactly, for example `greeting`, not `Greeting` |
| Compile error on the attribute | Braces are wrapped in quotes | Remove the quotes: `href={url}` |
| Compile error on the braces | The braces contain an expression such as `a + b` | Move the logic into the class |
| Value does not change on screen | The code changes a value that the template does not read, or changes an object in place | Reassign the field, or see the chapter on `@track` |
Where binding looks
The template can read any property on the component instance. Fields, getters, and public @api properties are all valid targets. It cannot read local variables from inside a method, and it cannot call methods. {formatName()} is not allowed. If a value needs to be calculated, give it a name on the class and bind to that name.
Exercise
A component class has the field title = 'Inbox'. Which template line correctly binds it to the aria-label attribute of a <section>, and what is wrong with <section aria-label="{title}">?
Show answer
Use <section aria-label={title}>. The version with quotes wraps the braces in quotes, which LWC rejects because binding values must be written without quotes.
Exercise
Why does <p>{firstName + ' ' + lastName}</p> fail to compile, and where should the combined value be built?
Show answer
Template braces accept only a property name (optionally with dotted access), not an expression. Build the combined value in the JavaScript class and bind the template to that property.