DEV Community

Connie Leung
Connie Leung

Posted on

3 1

New Angular 19 feature - Track expression requiring temporary variables

Angular 19.1.6 introduces track expression requiring temporary variables feature where @for's tracking expression can use coalesce (??) or logical (||) operator. It is useful when the previous value is null or undefined, and the next defined value is used to identify the array item.

In this demo, I define an Address type that consists of postal code and zip properties. The type of both fields is string | undefined. This is because the US address only has a zip code, a Canadian address only has a postal code, and a Hong Kong address has neither. The properties are mutually exclusive. When rendering an address array, @for's tracking expression comprises the zip and postal code to track the data row.

Data Type

// address.type.ts

export type Address = {
   line1: string;
   zip?: string;
   postalCode?: string;
   city: string;
   country: string;
}
Enter fullscreen mode Exit fullscreen mode

The type has required line1, city, and country while zip and postalCode are optional properties.

Sample Data

import { Address } from "./address.type";

export const addresses: Address[] = [
 {
   line1: 'Address 1',
   zip: '123456',
   city: 'New York',
   country: 'United States of America'
 },
 {
   line1: '150 Graydon Hall Drive',
   postalCode: 'M3A3B1',
   city: 'North York',
   country: 'Canada'
 },
 {
   line1: '150 Nathan Road',
   city: 'Mong Kok',
   country: 'Hong Kong'
 },
 {
   line1: '1 Middle Road',
   city: 'Tsim Sha Road',
   country: 'Hong Kong'
 },
 {
   line1: '197 Yonge Street, Massey Tower',
   postalCode: 'M5B0C1',
   city: 'Toronto',
   country: 'Canada'
 }
];
Enter fullscreen mode Exit fullscreen mode

The addresses array includes addresses in the USA, Canada, and Hong Kong. Since Hong Kong does not have a zip or postal code, I use the line1 field to track the array items.

Next, I can create a @for block in the inline template to display the data.

Initialize components with the array

// address-list.component.ts

@Component({
 selector: 'app-address-list',
 imports: [AddressItem],
 template: `
   @for (address of addresses(); track address.zip || address.postalCode || address.line1) {
     <app-address-item [address]="address" />
   }
 `,
 changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AddressList {
 addresses = input<Address[]>([]);
}
Enter fullscreen mode Exit fullscreen mode

The tracking expression is address.zip || address.postalCode || address.line1. When the zip value is defined, Angular uses it for tracking. When zip is undefined, and the postal code has value, postal code is the key to identify the array item. When both zip and postalCode are undefined, the key of that item is line1, which is always not blank.

@Component({
 selector: 'app-root',
 imports: [AddressList],
 template: `
    <app-address-list [addresses]="addresses"/>
 `,
 changeDetection: ChangeDetectionStrategy.OnPush,
})
export class App {
 addresses = addresses;
}
Enter fullscreen mode Exit fullscreen mode

The AppComponent imports the addresses array and passes it to the AddressList component as input. The AddresssList component finally loops the array to render the values.

Tip: The rows in the address array have unique zip, postal code, and line1. Therefore, address.zip || address.postalCode || address.line1 is a unique composite key. In a real-world application, these properties can easily have duplicate values in a database; therefore, the items should have unique IDs to distinguish them.

References:

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (0)

Postmark Image

The email service that speaks your language

Whether you code in Ruby, PHP, Python, C#, or Rails, Postmark's robust API libraries make integration a breeze. Plus, bootstrapping your startup? Get 20% off your first three months!

Start free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay