简体   繁体   中英

Ionic 2 reference a value in html file from ts file?

I am querying a set of data from firebase and successfully pulled a list of data to my html page. Is there any way to reference the corresponding item.uid value from the html page in my onViewProfile function so that I can push it to another page? Thanks. emphasized text

HTML file

<ion-content padding>
   <ion-label>Type</ion-label>
    <ion-select [(ngModel)]="type" (ionChange)="searchPitchesByType(type)">
      <ion-option value="test1">test1</ion-option>
      <ion-option value="test">test</ion-option>
    </ion-select>
<ion-list>
    <ion-item *ngFor="let item of pitches | async">
      Name: {{ item.name }}
      <p>
        Description:{{ item.description }}
      </p>
      <p>
        uid:{{ item.uid }} 
      </p>
      <button right ion-button icon-only (click)="onViewProfile()">
        <ion-icon name="add"></ion-icon>
      </button>
   </ion-item>
  </ion-list>

</ion-content>

TS file

export class DiscoverPage {
   public pitches: FirebaseListObservable<any>;

  constructor(
  private navCtrl: NavController,
   private loadingCtrl: LoadingController,
   private popoverCtrl: PopoverController,
    public afDatabase: AngularFireDatabase
    ) {this.pitches = afDatabase.list('/Pitches/');}

searchPitchesByType(type: string){

  this.pitches = this.afDatabase.list('Pitches', {
    query: {
      orderByChild: 'type',
      equalTo: type

    }

  })



}
onViewProfile() {
  let data = {
    uid: **(uid that corresponds to the uid in html)**


  }
    console.log(this.navCtrl.push(ViewProfilePage, data));
}
}

Well it is so simple that I feel I'm not getting it right.

You have to pass it as a parameter like this

<ion-item *ngFor="let item of pitches | async">
      Name: {{ item.name }}
      <p>
        Description:{{ item.description }}
      </p>
      <p>
        uid:{{ item.uid }} 
      </p>
      <button right ion-button icon-only (click)="onViewProfile(item)">
        <ion-icon name="add"></ion-icon>
      </button>
</ion-item>

Then your ts will turn into

onViewProfile(item) {
  let data = {
    uid: item.uid


  }
    console.log(this.navCtrl.push(ViewProfilePage, data));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM