0

Hi i have a problem with ListView in NativeScript using Angular2.

in my directive i have Array of Objects

export class Room {
constructor(
    public name: string,
    public tag: string
    ){ }
}

import { Page } from "ui/page";
import { Component, OnInit, EventEmitter, Output, ChangeDetectionStrategy } from '@angular/core';
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";

@Component({
    moduleId: module.id,
    selector: 'rooms-list',
    templateUrl: "./template.html",
    providers: [RoomService],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class RoomsListComponent implements OnInit {

    public items: Array<Room> = [];

    listLoaded = false;

    load() {
        for (let i = 0; i < 200; i++) {
                this.items.push(
                    new Room("room " + i, "tag " + i)
                );

            }
    }
    ngOnInit(): void {
        this.load();
    }
    onTap(name){
        console.log('taped: '+name);
    }
}

in my template i use ListView

<ListView [items]="items" id="roomsList">
    <template let-item="item">
        <StackLayout>
            <Label [text]='"[" + item.tag +"] " + item.name'></Label> 
        </StackLayout>
    </template>
</ListView>

but it looks like template tag doesnt exist, list contain only [object Object] elements.

3
  • 1
    Your code looks fine? What version of nativescript do you use? Commented Apr 11, 2017 at 7:33
  • 1
    Yes what version are you using, if you are using the RC (meaning Angular 4) the <template> has been changed to <ng-template>, similar question here: stackoverflow.com/a/43074761/3801632 Commented Apr 11, 2017 at 8:44
  • @VladimirAmiorkov with ng-template its still doesnt work. Im using nativescript 2.5 Commented Apr 11, 2017 at 9:56

2 Answers 2

0

When using the moduleId: module.id in a @Component you need to give only the name of the templateUrl without any directories. Using the moduleId makes it so that any Url is looked in the current directory so you no longer need to specific the absolute path. Simply do the following:

@Component({
    moduleId: module.id,
    selector: 'rooms-list',
    templateUrl: "template.html",
    providers: [ RoomService ],
    changeDetection: ChangeDetectionStrategy.OnPush
})

More information can be found in this thread, you can also take a look at the official NAtiveScript ng examples here.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you try a simpler binding expression like [text]="item.tag" just to make sure that the items of the ListView are actually populated. Also can you edit your post to include the source of the Room object, maybe there is something there.
i fixed it by importing NativeScriptModule from nativescript-angular/nativescript.module in my ngModule. before that i import this in component
0

The below works in NativeScript/Angular 5, should also in Angular 2 I think. Change:

<StackLayout>

to:

<StackLayout (tap)="onTap(item)">

and:

onTap(name){
    console.log('taped: '+name);
}

to:

onTap(item){
    console.dir(item); // So item is a Room object
}

Angular 5 would need ng-template instead of template as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.