NativeScript:未呈现的简单模板

问题描述 投票:0回答:1

通过* ngFor调用模板时,无论多么简单,都不会被渲染。

component.html:

<Label text="hello world""></Label>

container.html:

<StackLayout>
    <GridLayout *ngFor="let obj of objs">
        <!-- WORKS: -->
        <Label text="hello world"></Label>
        <!-- DOES NOT WORK: -->
        <ns-component></ns-component>
    </GridLayout>
</StackLayout>

当使用(简单的,非模板标签)下的片段,对于3个对象的数组,我看到三行“hello world”。但是,在调用模板而不是简单Label时,不会呈现任何内容。

nativescript angular2-nativescript
1个回答
0
投票

确保已在相应模块中注册了组件。

例如

使用选择器ns-component创建组件

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

@Component({
    selector: "ns-component",
    moduleId: module.id,
    template: '<Label text="ns-component: Some Content"></Label>',
    styleUrls: ['./item.component.css']
})
export class ItemComponent  { }

然后在加载的模块中注册组件

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptCommonModule } from "nativescript-angular/common";
import { NativeScriptFormsModule } from "nativescript-angular/forms";

import { HomeRoutingModule } from "./home-routing.module";
import { HomeComponent } from "./home.component";

import { ItemComponent } from "./item.component"; // HERE

@NgModule({
    imports: [
        NativeScriptCommonModule,
        HomeRoutingModule,
        NativeScriptFormsModule
    ],
    declarations: [
        HomeComponent,
        ItemComponent // HERE
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class HomeModule { }

最后根据需要使用* ngFor结构指令。

<StackLayout>
    <GridLayout rows="100" *ngFor="let obj of [1, 2, 3, 4]" class="cell">
        <ns-component></ns-component>
    </GridLayout>
</StackLayout>

Playground app demonstrating the above here.

© www.soinside.com 2019 - 2024. All rights reserved.