【发布时间】:2018-05-26 06:02:09
【问题描述】:
我是 nativescript angular2 的新手。到目前为止,我遵循了文档和 用listview做静态数据。
我把我的 json 文件放在了
app/utils/countries.json。我不知道如何获取本地文件路径来解析 json.Need 建议。
下面我发布了静态数据列表视图的代码。
打字稿:
import { Component, ElementRef, OnInit, ViewChild } from "@angular/core";
@Component({
selector: "ns-app",
templateUrl: "app.component.html",
})
export class AppComponent implements OnInit {
arrList: Array<Object> = [];
ngOnInit() {
this.arrList.push({ name: "India" });
this.arrList.push({ name: "Sri Lanka" });
}
}
HTML:
<page-router-outlet></page-router-outlet>
<GridLayout>
<ListView [items]="arrList" class="small-spacing">
<ng-template let-item="item">
<Label [text]="item.name" class="medium-spacing"></Label>
</ng-template>
</ListView>
</GridLayout>
countries.json: (app/utils/countries.json):
{
"countries": [
{"id":1,"name":"india"},
{"id":2,"name":"Sri Lanka"}
]
}
【问题讨论】:
-
您要导入您在
app/utils文件夹中的 json 文件吗? -
@mast3rd3mon 是的。我需要解析这个目录 app/utils/countries.json 中的 countries.json 文件。
标签: angular typescript nativescript angular2-services