Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store ` keyvalue` in the template variable

I am trying to use the keyvalue and print in template. but not able to assign with variable. what would be the correct approach here?

<h1 let item = (data|keyvalue)>New value {{item.key}}</h1> – not working

in my ts file there is a data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data = { value: 'info' };

I can directly print this. but to know how it can be handled with keyvalue

Thanks in advance.

>Solution :

The keyvalue pipe return an array of key-value-pairs. Only in your example the object has a single entry. Either iterate the resulting array with *ngFor or access the first array element like below.

<h1 *ngIf="(data | keyvalue)?.[0] as item">
  New value {{ item.key }}
</h1>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading