json - 按特定值过滤 JSON ionic 2

标签 json angular ionic2 angularjs-filter

我是 Ionic 2 和 Angular 的新手,我尝试过滤 Json 以仅显示特定值;示例仅按性别获取用户。这是我的代码

home.html

<ion-list>
    <ion-item *ngFor="let item of items | filter: {item.gender:'female'}" (click)="itemClicked($event,item)">
      <ion-avatar item-left>
        <img src="{{item.picture.thumbnail}}">
      </ion-avatar>
      <h2>{{item.name.first | uppercase }}</h2>
      <h3>{{item.name.last | uppercase }}</h2>
      <p>{{item.gender}}</p>
      <button ion-button item-right color="danger" (click)="buttonClick($event)">Button</button>
    </ion-item>
  </ion-list>

这是我的 API 服务 .ts 文件

    import { Component } from '@angular/core';
import {Http} from '@angular/http';
import { NavController } from 'ionic-angular';
import 'rxjs/add/operator/toPromise';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
public items:any;
  constructor(public navCtrl: NavController,public http: Http) {
      this.http = http;
        this.http.get("http://api.randomuser.me/?results=10")
            .subscribe(data =>{
              //console.log(data['_body']);
             this.items=JSON.parse(data['_body']).results;//Bind data to items object
            },error=>{
                console.log(error);// Error getting the data
            } );
  }
 buttonClick(event){
   console.log("button clicked");
   console.log(event);
  }

  itemClicked(event,itemData){
    console.log("item clicked");
    console.log(event);
    console.log(itemData);
  }
}

但是我的想法行不通 有什么想法可以帮助我-?? :`/

最佳答案

您可以在组件代码中而不是在 View 中执行此操作...

import { Component } from '@angular/core';
import {Http} from '@angular/http';
import { NavController } from 'ionic-angular';
import 'rxjs/add/operator/toPromise';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

    public items: any;

    constructor(public navCtrl: NavController, public http: Http) {
        // this.http = http; <- You don't need this, this.http is already available because by declaring it in the constructor, now a public property is created in the component
        this.http.get("http://api.randomuser.me/?results=10")
            .map(data => data.json().results) // Instead of getting the _body manually, you can use the map method from RxJS
            .subscribe(data =>{
                //console.log(data);
                this.items = data.filter(item => item.gender === 'female');
             },error=>{
                 console.log(error);// Error getting the data
             });
     }


     // ...

}

现在在你看来

  <ion-list>
    <ion-item *ngFor="let item of items" (click)="itemClicked($event,item)">
      <ion-avatar item-left>
        <img src="{{ item.picture.thumbnail }}">
      </ion-avatar>
      <h2>{{ item.name.first | uppercase }}</h2>
      <h3>{{ item.name.last | uppercase }}</h2>
      <p>{{ item.gender }}</p>
      <button ion-button item-right color="danger" (click)="buttonClick($event)">Button</button>
    </ion-item>
  </ion-list>

关于json - 按特定值过滤 JSON ionic 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43312724/

相关文章:

angular - 使用 Angular 版本 4 启动新的 Ionic 3 项目

Angular 2 Observable.forkJoin this._subscribe 不是 [null] 中的函数

javascript - Ionic 2(点击)触发两次 iOS 10.3.1

angular - ionic 3 Angular ionic slider 垂直对齐顶部

jquery - 如何在 angular2 中从 jQuery 获取样式

java - JSON 解析器仅获取 JSON 的一部分

java - Spring mvc Controller 问题

c# - 我在这里制造泄漏吗?

Angular HttpInterceptor - 处理异步响应

json - MongoDB/Node : Insert part of a doc in a collection into a doc in another collection