Android [Angular2][NativeScript]模板标记在ListView中不起作用

Android [Angular2][NativeScript]模板标记在ListView中不起作用,android,angular,listview,nativescript,Android,Angular,Listview,Nativescript,您好,我对使用Angular2的NativeScript中的ListView有问题 在我的指令中,我有一个对象数组 export class Room { constructor( public name: string, public tag: string ){ } } import { Page } from "ui/page"; import { Component, OnInit, EventEmitter, Output, ChangeDetectionSt

您好,我对使用Angular2的NativeScript中的ListView有问题

在我的指令中,我有一个对象数组

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);
    }
}
导出教室{
建造师(
公共名称:string,
公共标记:字符串
){ }
}
从“ui/Page”导入{Page};
从“@angular/core”导入{Component,OnInit,EventEmitter,Output,ChangeDetectionStrategy};
从“nativescript/router”导入{NativeScriptRouterModule};
从“nativescript/nativescript.module”导入{NativeScriptModule};
@组成部分({
moduleId:module.id,
选择器:“房间列表”,
templateUrl:“./template.html”,
提供者:[客房服务],
changeDetection:ChangeDetectionStrategy.OnPush
})
导出类RoomsListComponent实现OnInit{
公共项:数组=[];
listLoaded=false;
加载(){
for(设i=0;i<200;i++){
这个。项目。推(
新房间(“房间”+i,“标签”+i)
);
}
}
ngOnInit():void{
这个.load();
}
onTap(名称){
console.log('taped:'+名称);
}
}
在我的模板中,我使用ListView

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

但看起来模板标记不存在,列表只包含
[object object]
元素

@组件中使用
moduleId:module.id
时,只需给出
模板URL的名称,不需要任何目录。使用moduleId可以在当前目录中查找任何Url,因此不再需要指定绝对路径。只需执行以下操作:

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

更多信息可以在thread中找到,您也可以查看官方的NAtiveScript ng示例。

以下内容适用于NAtiveScript/Angular 5,我认为也适用于Angular 2。更改:

<StackLayout>
致:


Angular 5需要ng模板,而不是模板。

您的代码看起来不错吧?您使用的是哪个版本的nativescript?是的,您使用的是哪个版本,如果您使用的是RC(意思是Angular 4),则
已更改为
,类似的问题如下:@vladimirimiorkov with ng template它仍然不起作用。我使用的是nativescript 2.5,您可以尝试一个更简单的绑定表达式,比如
[text]=“item.tag”
,以确保ListView中的项目确实已填充。您还可以编辑您的帖子,以包含
房间
对象的源代码,可能其中有一些内容。我通过在ngModule中从nativescript angular/nativescript.module导入nativescript module来修复它。在此之前,我在组件中导入这个
<StackLayout (tap)="onTap(item)">
onTap(name){
    console.log('taped: '+name);
}
onTap(item){
    console.dir(item); // So item is a Room object
}