Show hide in ionic framework

断了今生、忘了曾经 提交于 2019-12-13 20:11:06

问题


I want a show hide function for my ionic app:

Below is what I have done so far, in xyz.html file:

<ion-item>    
<p class="font_c_2 gra_reg" (click)="onPtagClick(part.reg_id)"  *ngIf="!PtagClicked">
          {{part.fsp_partner_location}}
</p>
<p class="font_c_2 gra_reg" *ngIf="PtagClicked" (click)="onPtagClick1(part.reg_id)"  style="white-space:normal;">
          {{part.fsp_partner_location}}
</p>
</ion-item>

my xyz.ts file

export class xyzpage{
public PtagClicked: boolean = false;

public onPtagClick(id) {         
    {        
     this.PtagClicked = !this.PtagClicked;          
    }           
  }
 public onPtagClick1(id) {
   {            
    this.PtagClicked = false;   

    }           
  }
}

My problem is, I have dynamic numbers of created on this page, and if I click on 1 item, it show/hide all item's data and not the one I clicked for.

I guess the problem will be solved if I can create dynamic values for ngIf, but I tried and not able to as I am new to ionic.

Any help will be appreciated.

I have latest IONIC installed.

Thanks


回答1:


Use the Renderer2 to addClass, setStyle, or whatever you want, passing the element reference to your click event handler

Here a Demo

HTML

<ion-item *ngFor="let item of items">
  <h3 #text>{{item}}</h3>
  <button ion-button item-right (click)="onShow(text)">Show</button>
  <button ion-button color="danger" item-right (click)="onHide(text)">Hide</button>
</ion-item>

TS

import { Component, Renderer2 } from '@angular/core';
import { NavController } from 'ionic-angular';

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

public items: string[] = [
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5",
    "Item 6",
    "Item 7",
    "Item 8",
    "Item 9",
    "Item 10",   
];

  public PtagClicked: boolean = false;

  constructor(public navCtrl: NavController, private render: Renderer2) { }

  public onShow(controlToShow) {
    this.render.setStyle(controlToShow, 'visibility', 'visible');
  }
  public onHide(controlToHide) {
    this.render.setStyle(controlToHide, 'visibility', 'hidden');
  }

}


来源:https://stackoverflow.com/questions/49082846/show-hide-in-ionic-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!