【问题标题】:Show hide in ionic framework在离子框架中显示隐藏
【发布时间】:2018-08-11 11:28:42
【问题描述】:

我想要我的 ionic 应用程序的显示隐藏功能:

以下是我到目前为止所做的,在 xyz.html 文件中:

<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>

我的 xyz.ts 文件

export class xyzpage{
public PtagClicked: boolean = false;

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

    }           
  }
}

我的问题是,我在这个页面上创建了动态数字,如果我点击 1 个项目,它会显示/隐藏所有项目的数据,而不是我点击的那个。

如果我可以为 ngIf 创建动态值,我想问题将得到解决,但我尝试过但无法做到,因为我是 ionic 新手。

任何帮助将不胜感激。

我安装了最新的 IONIC。

谢谢

【问题讨论】:

    标签: javascript angular ionic-framework ionic3


    【解决方案1】:

    使用Renderer2 添加Class、setStyle 或任何你想要的,将元素引用传递给你的点击事件处理程序

    这里是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');
      }
    
    }
    

    【讨论】:

    • 感谢您的回复。这是为了“让 i of [1,2,3,4,5,6,7,8,9]”?
    • ngFor 将循环你传递的任何数组......像我的一样硬编码,或者来自模型。没关系。检查this to learn more
    • 这与唯一 ID 无关。您将引用传递给事件处理程序。整个 HTML 节点将被传递。当 ngFor 工作时,这在其特定上下文中是独一无二的。
    • 您设置了引用#text,然后将其作为变量传递给onShow(text)。如果你 console.log() onShow() 事件处理程序中的参数,你会看到传递了什么
    • 我刚刚使用模型编辑了答案。检查您现在是否更了解它
    猜你喜欢
    • 2015-11-23
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多