Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Elvis Operator within String Interpolated Expressions in Angular 2

In my Angular 2 app I am using string interpolation to pull data from a mongoDB. Right now the server/database in in flux, so on occasion that will break my client-side display because, for instance, if I am pulling in data via string interpolation, like this:

{{record.addresses[0].zipCode}}

... and then, at a later time, the way the data is organized in the database changes, this will break my display - because the client is trying to pull in from fields that are no longer there. So I think I should be able to use something like the elvis operator in a use case like this. That way, if the data is where the client is looking for it, it will print out to the screen. But, if the data is not where it's looking for it, it will just ignore the field altogether - not breaking anything in the display.

So, in short, how would I implement the elvis operator on an expression like I have above?

like image 726
Muirik Avatar asked Jan 12 '17 18:01

Muirik


People also ask

Can we use expressions Inside string interpolation in angular?

The angular evaluates the expressions into a string and replaces it in the original string and updates the view. You can use interpolation wherever you use a string literal in the view. Angular interpolation is also known by the name string interpolation. Because you incorporate expressions inside another string.

What is the syntax of string interpolation in angular?

String Interpolation in Angular 8 is a one-way data-binding technique that is used to transfer the data from a TypeScript code to an HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view.

Why do we use interpolation in angular?

In Angular, String interpolation is used to display dynamic data on HTML template (at user end). It facilitates you to make changes on component. ts file and fetch data from there to HTML template (component. html file).


1 Answers

You use it like this:

{{record?.addresses[0]?.zipCode}}

This will check if record is defined then if addresses[0] is defined under record object and then if zipCode is defined under that object

like image 96
eko Avatar answered Sep 30 '22 06:09

eko