Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法读取nativescript angular2中未定义的属性全局数组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我得到一个运行时错误,因为无法读取未定义的属性listArr.我需要在多个组件中重用相同的数组.这就是我使用Global数组的原因

添加了相关代码.请检查一下.

Global.ts:

export class Global {

    public static listArr: ObservableArray<ListItem> = new ObservableArray<ListItem>();

}

ts文件

Global.listArr.push(data.items.map(item => new ListItem(item.id,item.name,item.marks)));

html文件

<ListView [items]="Global.listArr"  > </ListView>
我建议你去共享服务.保持服务中的全局数组将服务标记为app.module中的提供者i:e您的主模块.
Service

import { Injectable } from '@angular/core';

@Injectable()
export class Service{

    public static myGloblaList: string[] = [];

    constructor(){}


}

将它添加到NgModule的providers数组中.

现在您可以在任何组件中使用它

constructor(private service : Service){
  let globalList = this.service.myGlobalList;// your list
}

我选择服务的原因是它使用Angular的依赖注入,它是拥有全局变量并在组件之间共享的最佳Angular方式.

如果您希望组件在推送和弹出时自动通知阵列中的更改,您可以使用Services.LINK-问题2中的行为主题

大佬总结

以上是大佬教程为你收集整理的无法读取nativescript angular2中未定义的属性全局数组全部内容,希望文章能够帮你解决无法读取nativescript angular2中未定义的属性全局数组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: