DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

3

Angular v17.2 Updates - viewChild()

Image description

ViewChild is a way to get a reference on an HTML within the current component’s template.

For instance, if we need our Typescript to access the instance of a given child component, We can use @ViewChild like this:



export class App {
  @ViewChild('list') listComponent!: ListComponent;
  ...
  console.log(this.listComponent.items);
}


Enter fullscreen mode Exit fullscreen mode

The new syntax gives us the same feature as a Signal:



export class App {
  listComponent = viewChild.required<ListComponent>('list');
  ...
  console.log(this.listComponent.items);
}


Enter fullscreen mode Exit fullscreen mode

👉 Differences between these new functions and the old decorators:

These new functions bring a few extra exciting features.

➖ We can eliminate possibly undefined values by making a query required.

➖ Decorator-based queries were satisfied once the component was fully initialized, meaning running side effects required specific lifecycle methods.

➖ With signal-based queries, we receive a signal to rely on computed() or effect() to run such side-effects.

An example is here: Angular viewChild() Demo


I hope you found it useful. Thanks for reading. 🙏

Let's get connected! You can find me on:

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay