Tips for getting Nativescript listview to function properly

I am currently developing an app using nativescript and angular 2. I am facing some issues while trying to implement the nativescript listview component. Whenever I run the app, all I see is " [object object] ".

Below is my view code :


<grid-layout columns="*" rows="*">
   <ListView [items]="itemsd" class="listview" #LISTV>
       <ListView.itemTemplate>
            <grid-layout columns="50, *" rows="*" class="item item-avatar">
                <image src="{{ '~/images/' + img }}" col="0"></image>
                <stack-layout col="1">
                    <label text="{{ artist }}" class="h2" col="1"></label>
                    <label text="{{ title }}" class="p" col="1"></label>
                </stack-layout>
            </grid-layout>
       </ListView.itemTemplate>
   </ListView>
</grid-layout>

This is my typescript code :


ngOnInit(): void {
    this.fakeListItems = [{
        "img":"ANDY.jpg",
        "artist":"ANDY",
        "title":"A User"
    }]
    this.itemsd = new ObservableArray(this.fakeListItems);
}

I have tried multiple ways to resolve the issue with the listview component, however, due to unclear documentation and various versions from different sources, I am unable to find a solution. Can someone please guide me on how to make it work properly?

Thank you.

Answer №1

Perhaps a mix of syntaxes is causing the issue. Have you tried modifying your [items]="itemsd" to items="{{itemsd}}"?

Below is some Angular 2.3 NS code from one of my demo-apps that was functioning correctly:

  <ListView items="{{ peripherals }}" itemTap="{{ onPeripheralTap }}" separatorColor="#ffffff">
      <ListView.itemTemplate>
          <StackLayout orientation="horizontal">
              <StackLayout cssClass="padded-label-stack" width="60%">
                <Label horizontalAlignment="left" text="{{ advertisement }}"/>
                <Label horizontalAlignment="left" text="{{ number1 + ' coins for €' + number2 }}"/>
              </StackLayout>
              <StackLayout cssClass="padded-label-stack" width="40%">
                <Label horizontalAlignment="right" text="{{ 'Time: ' + timestamp }}"/>
                <Label horizontalAlignment="right" text="{{ distance + 'm away' }}"/>
              </StackLayout>
          </StackLayout>
      </ListView.itemTemplate>
  </ListView>

Your ts-code doesn't seem to significantly differ from mine, except that you aren't actively making your object Observable. You are initializing your array correctly.

observablePeripheralArray = new observableArray.ObservableArray();
peripheralArray = new Array();
peripherals = this.observablePeripheralArray;

var that = this;
var onDiscovered = function (result) {
  let extended = new ExtendedPeripheral(result);
  var obsp = new Observable(extended);
  ...
  that.observablePeripheralArray.splice(i, 0, obsp);

One thing to consider: It might be helpful to use Visual Studio Code with NativeScript plugins so you can debug and see exactly what your items are. Alternatively, you could try using JSON.stringify(this.itemsd[0]) to check if your structure is correct. If your objects always have the same fields, creating a class for them could also be beneficial.

Answer №2

It appears that there was a misunderstanding in the syntax used, switching from {{ }} to [ ]

<grid-layout columns="*" rows="*">
     <ListView [items]="itemsd" class="listview" #LISTV>
          <ListView.itemTemplate>
                <grid-layout columns="50, *" rows="*" class="item item-avatar">
                        <image [src]="'~/images/'+img" col="0" ></image>
                              <stack-layout col="1">
                                  <label [text]="artist" class="h2" col="1"></label>
                                  <label [text]="title" class="p" col="1"></label>
                               </stack-layout>
                 </grid-layout>
            </ListView.itemTemplate>
       </ListView>
</grid-layout>

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

MSBUILD encounters numerous JQuery errors when compiling a web project with TypeScript

Currently, I am working on a .net core 3.1 (netcoreapp3.1) razor pages project that includes typescript files and a few javascript files. The project builds perfectly from Visual Studio 2019 (professional) as well as from the command line using MSBuild. H ...

Error message: "IAngularStatic type does not have property IScope" caused by Typescript Directives

I'm working on creating an Angular Directive using TypeScript to share a scope item. I created an interface that inherits from ng.IScope, but Visual Studio Code is showing me a warning: "Property IScope does not exist on type IAngularStatic". I am usi ...

Using form-urlencoded data in Angular 5 applications

I have been working on an application that combines a Spring backend with an Angular 5 frontend. The login functionality is secured using Spring Security, and in the frontend, I am trying to send the login data as x-www-form-urlencoded. However, the backen ...

TypeScript: Defining a custom object type using an array of objects

Is there a way to dynamically generate an object based on the name values in an array of objects? interface TypeA { name: string; value: number; } interface TypeB { [key: string]: { value: any }; } // How can we create an OutputType without hard ...

Configuring global runtime variables in NextJS for server-side operations

Currently, I am utilizing the instrumentation.ts file in NextJS to retrieve configuration dynamically when the server starts up. My goal is to have this configuration accessible during runtime for all API routes and server-side components. What would be th ...

Ensuring multiple choices are validated within a reactive form in Angular

Having just started working with Angular, I encountered an issue regarding the validation of a user's gender. The default setting for gender is not specified in the form, which is used to update user data. In some cases, the user may choose to set the ...

What might be causing AngularJS to fail to display values when using TypeScript?

I have designed the layout for my introduction page in a file called introduction.html. <div ng-controller="IntroductionCtrl"> <h1>{{hello}}</h1> <h2>{{title}}</h2> </div> The controller responsible for handling th ...

What is preventing type-graphql from automatically determining the string type of a class property?

I have a custom class named Foo with a property called bar that is of type string. class Foo { bar: string } When I use an Arg (from the library type-graphql) without explicitly specifying the type and set the argument type to string, everything works ...

The error message "@graphql-eslint/eslint-plugin: problem with the "parserOptions.schema" configuration"

Our team is currently working on developing micro-services using NestJS with Typescript. Each of these services exposes a GraphQL schema, and to combine them into a single graph, we are utilizing a federation service built with NestJS as well. I recently ...

Conduct surveillance on the service function call within the constructor

I am currently facing a challenge with trying to monitor a service function call that is executed in the constructor. The test is straightforward, simply aiming to confirm that the function call is indeed made. beforeEach(async(() => { TestBed.con ...

Adjust ion-select label width across the entire screen in Ionic version 6

I recently began working on a fresh project using Ionic v6. As part of the development, I included a basic ion-select element in my HTML code: <ion-item> <ion-select placeholder="Select Option"> <ion-select-opti ...

Merging Promises in Typescript

In summary, my question is whether using a union type inside and outside of generics creates a different type. As I develop an API server with Express and TypeScript, I have created a wrapper function to handle the return type formation. This wrapper fun ...

How to create a TypeScript generic function that takes a key of an object as a type argument and returns the corresponding value of that key in the object

My system includes various object types: type Slave = { myKey:string } type AnotherSlave = { anotherKey:string } In addition, there is a master type that contains some keys, with the object types mentioned above as the values for those keys: type Mas ...

Adding data to an array from a JSON source using Angular 5

When I receive a response from an endpoint, it looks like this: { "data": [{ "volume": 4.889999866485596, "name": "Carton03", "weight": 5.75, "storage": 3 }, { "volume": 2.6500000953674316, "name": "Carton02", "weight": 4.5 ...

Tips for effectively utilizing typescript interface with the OR operator

I'm a beginner when it comes to TypeScript. I have the following code snippet that I am working on: interface InitialData { active: boolean; status: boolean; } interface Item { id: number; name: string; Data: InitialData; } interface Main ...

Removing AWS-CDK Pipelines Stacks Across Multiple Accounts

Currently, I am utilizing pipelines in aws-cdk to streamline the process of automating builds and deployments across various accounts. However, I have encountered an issue where upon destroying the pipeline or stacks within it, the respective stacks are ...

Is it possible for Next.js to retrieve the window size without resorting to a faulty hook call or encountering an undefined window

In my ongoing efforts to dynamically adjust the size of an image within a next.js application to make it responsive to various screen sizes, I have encountered challenges. The different methods I have attempted and observed have resulted in either an inv ...

Maintain Symbolic Links in Angular Libraries

Hey there! I'm facing an issue with creating an Angular 8 Library using ng-cli. I'm struggling to maintain symlinks when linking my external application with npm link. I attempted to modify my angular.json file like this: "build": { "bui ...

Exploring the integration of traditional service methodologies into modern Angular applications

I am tasked with converting various types of services like SOAP-based services, WCF XML services, and web services that are currently running in a Silverlight app. For this conversion, we plan to use Angular as the front end while keeping the server side u ...

Navigating JSON data in a POST request using C# in .NET Core version 3.1

Previously, in my .Net Core 2.1 project, I had implemented a method to handle JSON data successfully as shown below: [HttpPost] [Route("sendJsonData")] public JObject saveTemplate(JObject jsonString) { string templateName = (string)jsonString.Sel ...