Tips for showing JSON information in Nativescript

How can I display all values in a page using the provided JSON data?

res {
   "StatusCode": 0,
   "StatusMessage": "OK",
   "StatusDescription": [
     {
       "sensors": [
         {
           "serial": "sensor1",
           "id": "1"
         },
         {
           "serial": "sensor2",
           "id": "2"
         },
         {
           "serial": "sensor3",
           "id": "3"
         }
       ],
       "HBP_id": "12",
       "HB_id": "123",
       "serial_number": "hb1",
       "note": "test"
    }
   ]
}

I need to display the following in Nativescript:

serial_number: hb1
sensors : sensor1

I attempted to use the code below, but it only displays the serial number:

<StackLayout class="page">
    <ListView [items]="items" class="list-group">
         <ng-template let-item="item">
             <Label [text]="item.serial_number"
              class="list-group-item"></Label>
         </ng-template>
    </ListView>
</StackLayout>

Does anyone have an idea on how I can also display the sensors along with the serial number?

Thank you

Solution:

<ListView [items]="items" (itemTap)="onItemTap($event)">
                <ng-template let-item="item" let-i="index" let-odd="odd" let-even="even">
                    <StackLayout [class.odd]="odd" [class.even]="even">
                        <Label [text]="item.serial_number"></Label>
                        <Label *ngFor="let subItem of item?.sensors" [text]="subItem.serial"></Label>
                    </StackLayout>
                </ng-template>
            </ListView>

Answer №1

Have you considered utilizing nested <ng-template> elements?

<StackLayout class="page">
    <ListView [items]="items" class="list-group">
         <ng-template let-item="item">
             <Label [text]="item.serial_number"
              class="list-group-item"></Label>
             <ng-template *ngFor="let subItem of item?.sensors">
                  <Label [text]="subItem.serial"
                      class="list-group-item"></Label>
             </ng-template>
         </ng-template>
    </ListView>
</StackLayout>

If using ngFor is not an option, you can try using let-subItem="item?.sensors".

This approach might be useful for achieving your desired outcome!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to eliminate specific lines from a JSON file that share a common pattern?

Hey, I have a json file containing 5000 lines where some of them follow a pattern similar to this: [ {"a":"c", "b":"e", "i":"a"}, {"a":"c", "b":"/", "i":"a"}, {"a":"c", "b":"/", "i":"a"}, {"a":"c", "b":"e/esa", "i":"a"}, {"a":"i", "b":"a/e/", "i":"ah"}, ] ...

Struggling to modify a string within my React component when the state is updated

Having a string representing my file name passed to the react-csv CSVLink<> component, I initially define it as "my-data.csv". When trying to update it with data from an axios request, I realize I may not fully understand how these react components w ...

Having trouble sending a JSON object from Typescript to a Web API endpoint via POST request

When attempting to pass a JSON Object from a TypeScript POST call to a Web API method, I have encountered an issue. Fiddler indicates that the object has been successfully converted into JSON with the Content-Type set as 'application/JSON'. Howev ...

What could be the reason for the extra tags being returned by YQL?

Currently, I am executing a query in the YQL console with the following data: select * from html where url='http://www.motorni-masla.net/index.php?main_page=product_oil_info&cPath=140&products_id=294&zenid=c8281021bbfed454176247900b3b9d4a ...

Convert objects to JSON using ServiceStack serialization with a specific function

I have a C# helper class that defines a JavaScript component with a specific JavaScript function name. new Button({Label = "Execute", OnClick = "ExecuteFunction"}); This will generate a JSON text: { label = "Execute", onClick = ExecuteFunction} (where ...

The 'Headers' type does not share any properties with the 'RequestOptionsArgs' type

I have included the necessary headers, objects, and URLs in my code but I keep getting an error message. The 'Headers' type does not have any properties in common with the 'RequestOptionsArgs' type This is my code: getContactData(a ...

Tips for transferring TimeZone Name from Angular to .NET API

Currently, the API I am working with accepts TimeZone names (such as America/Denver) as a string. In my Angular UI application, I automatically pass the browser timeZoneName to the API. However, when the API receives the string America/Denver, it interpret ...

What is the process of extracting an observable from another observable using the pipe method?

Is there a more efficient way to convert an Observable of Observables into an array of Observables in my pipe method? Here is the scenario: // The type of "observables" is Observable<Observable<MyType>[]> const observables = this.http.get<M ...

Tips for handling catch errors in fetch POST requests in React Native

I am facing an issue with handling errors when making a POST request in React Native. I understand that there is a catch block for network connection errors, but how can I handle errors received from the response when the username or password is incorrec ...

Error message: The property 'data' is not recognized within this context. Additionally, the property 'datatime' does not exist on the current type

I'm currently working on generating a graph using Firestore data and ng2charts. However, when I run my code, I encounter the following errors: Property 'data' does not exist on type 'unknown', causing an error in item.data Simila ...

After routing, parameters are mistakenly being inserted into the URL

When working with Angular 5 and using routing to move between pages / components, I am facing an issue. Specifically, in the login component, after signing out I navigate to the Home component but unnecessary parameters are being added to the URL. Below i ...

The Nestjs ClientMqtt now has the capability to publish both pattern and data to the broker, as opposed to just sending

I am currently utilizing Nestjs for sending data to a Mqtt Broker. However, I am facing an issue where it sends both the pattern and data instead of just the data in this format: { "pattern": "test/test", "data": " ...

Exclude<Typography, 'color'> is not functioning correctly

Take a look at this sample code snippet: import { Typography, TypographyProps } from '@material-ui/core'; import { palette, PaletteProps } from '@material-ui/system'; import styled from '@emotion/styled'; type TextProps = Omi ...

I'm encountering an issue where the npm install process is getting stuck while attempting to extract the xxxx package

When running the sudo npm install command on my local machine, everything works fine. However, once I pulled the code into an EC2 Ubuntu machine, the npm install process gets stuck at a certain point. The output shows "sill doParallel extract 1103" and I ...

The Nx end-to-end Cypress runner ensures that values from the cypress.env.json file do not overwrite the same entries in the cypress.json environment object

Having encountered an issue with my Nx powered angular project, I am facing a situation where the values provided in a cypress.env.json file are not overriding the corresponding entries in the env object of the cypress.json configuration file. This is a n ...

NextJS: Error - Unable to locate module 'fs'

Attempting to load Markdown files stored in the /legal directory, I am utilizing this code. Since loading these files requires server-side processing, I have implemented getStaticProps. Based on my research, this is where I should be able to utilize fs. Ho ...

Is it possible for VSCode to automatically generate callback method scaffolding for TypeScript?

When working in VS + C#, typing += to an event automatically generates the event handler method scaffolding with the correct argument/return types. In TypeScript, is it possible for VS Code to offer similar functionality? For instance, take a look at the ...

Hello, I'm in need of assistance with solving an error issue that I keep encountering. I have been experiencing this problem consistently and am unable to start the remote webdriver

Error: Unable to determine type from provided data. Last 1 characters read: EBuild information - version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'System details: host: 'EX-CON-2255C', ip: &apos ...

Encountering a Typings Install Error When Setting Up angular2-localstorage in WebStorm

I am currently using Windows and WebStorm as my development environment. I attempted to install the angular2-localstorage package by running the command npm install angular2-localstorage, but encountered an error. After realizing that the angular2-localst ...

Launch the Image-Infused Modal

I am completely new to the world of Ionic development. Currently, I am working on a simple Ionic application that comprises a list of users with their respective usernames and images stored in an array. Typescript: users = [ { "name": "First ...