如何获取数组中某一项的id

How to get the id of an item in an array

我已经使用 PHP 作为我的后端语言从我的数据库中调用了一个项目数组,并且我已经在我的 ionic 视图中正确显示了它,但是我尝试记录每个项目的 ID,这样我就可以用它来执行其他任务,但它一直给我循环中第一项的 id

下面的代码是ionic中的ts代码,用于在页面init上获取数据

ngOnInit() {
  this.username = this.navParams.get('mobile');
  var headers = new Headers();
  headers.append("Accept", 'application/json');
  headers.append('Content-Type', 'application/json');
  let requestOptions = new RequestOptions({ headers: headers });
  let data = {
    userid: this.username
  };
  this.http.post('http://127.0.0.1:8000/api/myitems', JSON.stringify(data), requestOptions)
  //.timeout(40000)
  .map(res => res.json())
  .subscribe(res => {
    this.items =res.My_data.phone;
    console.log(this.items);
  });
}

下面是点击项目时的代码

Detail(item) {
  console.log(item);
}

这是ionic

视图中的html代码
<ion-item *ngFor="let item of items" on-click="Detail()">
  {{item.phone_name}} | {{item.model}} <br> {{item.serial_no}}
</ion-item>

每次我点击它时,项目的 id 应该像 9,10,11,12 一样循环,但它的 returns 只有 9

PHP 下面的代码(内置 laravel

public function index(Request $request){

    $car=caritem::where('ownerid', $request->userid)->get();
    $comp=Computer::where('ownerid', $request->userid)->get();
    $phone = useritem::where('ownerid', $request->userid)->get();
    $my_data = ['status' => 200, 'Author\'s Name' => "Ademola Segun", 'Api Version' => "v1.0",
        'Purpose' => "Findit App API", 'car' => $car, 'comp'=>$comp, 'phone'=> $phone];
    return response()->json(['My_data'=>$my_data]);

}

首先,你的 Detail () 函数正在等待一个参数,而你的 html 你没有给它,

像这样更新您的 html:

<ion-item *ngFor="let item of items" on-click="Detail(item)"> .... </ion-item>

在你的 ts 中你应该得到一个 id 为

的对象