【问题标题】:Angular4 - show/hide content inside a loop [duplicate]Angular4 - 在循环内显示/隐藏内容[重复]
【发布时间】:2018-06-16 01:38:43
【问题描述】:

我开始学习 Angular 4。 我有多个 div 部分,每个 div 部分都有按钮和内容,单击单击的按钮将显示相应的 div,单击另一个按钮隐藏其他按钮。

<div *ngFor="let number of [0,1,2,3,4] ; let i = index">
  <div>
    <button (click)="showContent(this);"> Click Me {{index}} </button
    <div class="content"> Hello world {{index}}</div>
  </div>      
</div>

js代码

export class AppComponent {

  showContent(evt){

    event.target.style.display = 'block';   
  }   

}

抱歉,我只添加了最少的代码。

【问题讨论】:

  • 注意标签,这是 Angular,而不是 AngularJS。也没有Angularjs 4这样的东西;)
  • 好的,我更新了问题。
  • 我建议您先阅读一些基本的 Angular 文档,然后再在这里提出非常基本的问题。
  • Angular 很少需要做 DOM 操作,你应该尽量避免它
  • 好的,您能否建议单击按钮时隐藏/显示相应内容的方式?

标签: javascript angular angular-template


【解决方案1】:

您可以使用 &lt;div *ngIf="variable"&gt; 和 variable 作为布尔值。您的函数showContent() 可以将variable 更改为true,并且您的组件将被显示。

【讨论】:

  • 我在 ngFor 中有多个 div。
  • 你可以使用&lt;div *ngIf="array[i]"&gt; 并设置一个布尔数组。
【解决方案2】:

这可能会有所帮助:

<div *ngFor="let number of [0,1,2,3,4] ; let i = index">
  <div>
    <button (click)="showContent(this, index);"> Click Me {{index}} </button
    <div class="content" *ngIf="selectedIndex == index"> Hello world {{index}}</div>
  </div>      
</div>

JS:

export class AppComponent {
  selectedIndex = -1;
  showContent(evt, index) {
    this.selectedIndex = index;   
  }
}

【讨论】:

    【解决方案3】:

    这可能会有所帮助:

    <ng-container *ngFor="let item of [1,2,3,4];let i=index">
        <div style="height:40px;border:1px solid #000;margin-bottom:10px">
            <button (click)="selectedDivIndex=i">Button {{i+1}}</button>
            <div class="content" *ngIf="selectedDivIndex===i">
                Content for #{{i+1}}
            </div>
        </div>
    </ng-container>
    

    注意:如果您想最初加载内容,请将selectedDivIndex 设置为组件文件中的某个默认索引(.ts)

    【讨论】:

      【解决方案4】:

      解决这个问题有不同的方法:

      1- 创建另一个数组来保留每个项目的状态,我不喜欢:

      public state = ['none','none','none','none','none'];  
      
      
        <div *ngFor="let number of [0,1,2,3,4] ; let i = index">
          <div>
            <button (click)="state[i]='block'"> Click Me {{index}} </button
            <div [style.display]='state[i]' class="content"> Hello world {{index}}</div>
          </div>      
        </div>
      

      2- 创建一个封装逻辑并且可重用的指令:

      @Directive({
      
         selector:`[showHide]`,
         exportAs :'showHide'
      
      })
      
      export class ShowHideDirective{
      
        @Input('showHide') isHidden = true; // initial state
      
      
        public show(){
      
             this.isHidden = false;
        }
      
      
      
        @HostBinding('[atrr.hidden]') get isItHidden() { // if this is not working for you, you can change it to `[style.display]` and return the `block` or `none`
           return this.isHidden;
        };
      
      
      }
      
      
      <div *ngFor="let number of [0,1,2,3,4] ; let i = index">
        <div>
          <button (click)="showHideElement.show()"> Click Me {{index}} </button
          <div #showHideElement='showHide' class="content"> Hello world {{index}}</div>
        </div>      
      </div>
      

      别忘了在你的应用模块中声明ShowOnClickDirective。

      【讨论】:

      • 有一点不同,display:none 不会从 DOM 中移除节点
      • @Jota.Toledo 是的,但我看不出他是想删除它还是简单地隐藏它,根据他的代码 sn-p 他想隐藏它
      • @Milad,一旦点击“点击我”按钮,它会显示相应的内容,但同时需要隐藏其他内容。有可能吗?
      • @RSKMR 把 github 发给我,我帮你写代码
      猜你喜欢
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 2014-07-08
      • 1970-01-01
      相关资源
      最近更新 更多