【问题标题】:get string value from observable array in angular2 nativescript从angular2 nativescript中的可观察数组获取字符串值
【发布时间】:2018-01-15 22:36:57
【问题描述】:

我将 json 数据推送到 observable 数组中。我需要从 ShowData 中获取唯一的地址。这意味着我只需要一个字符串类型,我需要根据位置获取地址值。

ShowData.ts:

 class ShowData{

  constructor(public id:number, public name:string, public address:string, public code:string) {
  }

 }

ts 文件:

  private arrList: ObservableArray<ShowData> = new ObservableArray<ShowData>();

openData(pos : number){    --->listview item position


   let getValue: any = this.arrList.get(pos);  // this is not worked


}

根据listview项位置,我只需要得到arrList地址。

【问题讨论】:

  • ObservaleArray 是 NativeScripts Observables 的一部分,与 RxJS 的 Observables 和 Angular 不兼容。

标签: angular typescript nativescript angular2-nativescript


【解决方案1】:

可观察数组使用.getItem 从数组中检索项目(不是.get

openData(pos : number){   
   let getValue: any = this.arrList.getItem(pos).address;
}

【讨论】:

    【解决方案2】:

    如果你想要地址,下面会有所帮助

    openData(pos : number){    --->listview item position
       let getValue: any = this.arrList[pos].address;
    }
    

    【讨论】:

    • this.arrList.getItem(pos).address ;)
    • 在控制台中我无法读取未定义的属性地址。 console.log("地址", getValue);
    • 您确定您传递给函数的pos 有效吗?即你确定pos 不大于数组长度吗?
    • 我会检查并通知您。
    • @NicolaeSurdu 你的评论this.arrList.getItem(pos).address 做得很好。谢谢你的回答。您可以将此作为答案发布吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2023-03-28
    • 2017-12-23
    • 2016-06-04
    • 2017-04-16
    • 1970-01-01
    相关资源
    最近更新 更多