4

I'm building a ListView based component, similar to the groceries example in the {N} page. I have a "+" button, that needs to add new items to the list, I have this code:

import { Component, OnInit } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'my-list',
    templateUrl: 'my-list.component.html'
})
export class ListComponent implements OnInit {
    private myList: CustomObject;
    constructor() { }

    ngOnInit() { }

    addItem(){
        this.myList.push(new CustomObject());
    }

}

And here is the template:

<StackLayout>
    <Button text="+" (tap)="addItem()" ></Button>
    <ListView [items]="myList">
        <template let-item="item" let-i="i">
            <Label text="item.name"></Label>
        </template>
    </ListView>
</StackLayout>

My problem is, when I click on the "+" button, I get an undescifrable exception. When I fill the list with code, no problem, but I need the user can add new elements to the view. How is the correct way to implement a dynamic ListView like I described?

EDIT:

An uncaught Exception ocurred on "main" thread. com.tns.NativeScriptException: Calling js method getView failded

Error No suitable views found in list template! Nesting level:0 File: "/data/data/org.nativescript.MyApp/files/app/tns_moudules/nativescript-angular/directives/list-view-comp.js, line:135 column:8

StackTrace: Frame: function:'getSingleViewRecursive', file:....

2
  • 1
    What do you mean with "undescifrable"? To undescifrable to be able to post it here? Commented Nov 30, 2016 at 11:46
  • I mean there are a lot of lines on the StackTrace referencing to .js files. I post the first lines (for some reason I can't copy all the exception on the clipboard) Commented Nov 30, 2016 at 12:05

1 Answer 1

4

In NativeScript + Angular-2 application you can use AsyncPipe

Example on how to provide data via async pipe in NativeScript + NG2 app can be found here

What is noticeable is the usage of RxObservable

page.component.ts

import { Component, ChangeDetectionStrategy } from "@angular/core";
import { Observable as RxObservable } from "rxjs/Observable";

export class DataItem {
    constructor(public id: number, public name: string) { }
}

@Component({
    templateUrl: "ui-category/listview/using-async-pipe/using-async-pipe.component.html",
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UsingAsyncPipeComponent {
    public myItems: RxObservable<Array<DataItem>>;

    constructor() {
        let items = [];
        for (let i = 0; i < 3; i++) {
            items.push(new DataItem(i, "data item " + i));
        }

        let subscr;
        this.myItems = RxObservable.create(subscriber => {
            subscr = subscriber;
            subscriber.next(items);
            return function () {
                console.log("Unsubscribe called!");
            };
        });

        let counter = 2;
        let intervalId = setInterval(() => {
            counter++;
            items.push(new DataItem(counter + 1, "data item " + (counter + 1)));
            subscr.next(items);
        }, 1000);

        setTimeout(() => {
            clearInterval(intervalId);
        }, 15000);
    }
}

page.component.html

<ListView [items]="myItems | async" class="list-group">
    <template let-item="item" let-i="index" let-odd="odd" let-even="even">
        <GridLayout class="list-group-item" [class.odd]="odd" [class.even]="even">
            <Label [text]="item.name" android:class="label-item"></Label>
        </GridLayout>
    </template>
</ListView>

In this basic example the async is simulated with setInterval but based on the same logic you can achieve your desired UX with a button.

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

1 Comment

Tweeking this code a little, i achieved what I want. My problem now is that if I ad a custom component instead of a label in the template, I get the same exception. Is there any site where explains how this works? Thanks!

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.