【问题标题】:building divs in loop and show them on button press angular在循环中构建 div 并在按钮按下角度时显示它们
【发布时间】:2020-12-18 09:25:59
【问题描述】:

我想循环制作 div 并通过按下特定按钮显示我选择的 div。 从理论上讲,我看到如下...... 使用 *ngFor 我为它们构造 div 和按钮 让我们说

<div>div1</div><button (click)="showDiv(divID)">showDIV</button>

要隐藏 div 我必须使用 ngIf

 <div *ngIf="Here I must place something wih divID">>div1</div><button (click)="showDiv(divID)">showDIV</button>

并且在 ts 文件中,当我按下它的按钮时,我必须将具有 divID 的值设置为 TRUE。

那么我该怎么做呢,我想它必须是数组或其他东西。 请给我必须在模板和ts文件中写入的解决方案示例

用于动态数组构建的 ts 文件的一部分。

data.length=6
i=1
        data.forEach(element => {
Here I want to add variable to array
          this.i++;
        });

【问题讨论】:

  • 看起来你只需要一个开关按钮,状态为“on”和“off”。目前尚不清楚您是否需要在父组件中控制该状态,所以我宁愿说一个通知事件就可以了?看看巴西同胞的这个例子:stackblitz.com/edit/…

标签: arrays angular angular-ng-if


【解决方案1】:

您可以通过以下方式实现您的要求。

请参考以下示例 示例代码:

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  divisions = [
    {
      content: 'Div 1 content .......',
      visibility: false
    },
    {
      content: 'Div 2 content .......',
      visibility: false
    },
    {
      content: 'Div 3 content .......',
      visibility: false
    },
    {
      content: 'Div 4 content .......',
      visibility: false
    },
  ];

  toggleDiv(index: number) {
    this.divisions[index].visibility = !this.divisions[index].visibility;
  }
}

app.component.html

<div class="container">
  <ng-container *ngFor="let div of divisions;let i=index">
    <div class="div-btn">
      <button type="button" (click)="toggleDiv(i)" class="btn btn-primary">{{div.visibility===false? 'Show': 'Hide' }}
       Div {{i+1}}</button>
    </div>
    <div *ngIf="div.visibility" class="div-content">{{div.content}}</div>
  </ng-container>
</div>

app.component.css

.div-content {
  height: 100px;
  border: 1px solid #ccc;
  padding: 10px;
  margin: 10px;
}

.div-btn{
  padding: 10px;
  margin: 10px;
}

【讨论】:

  • 非常感谢先生。我已经在 Michael 的帮助下解决了我的问题,但非常感谢你的努力,你用所有这些动画和细节创造了非常好的答案。再次感谢您,先生!
【解决方案2】:

你可以在控制器中定义一个数组

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  buttons = [];

  ngOnInit() {
    for (let i=1; i<=5; i++) {
      this.buttons.push({
        id: i, show: false
      });
    }
  }
}

并用它在模板中动态显示按钮

<div *ngFor="let button of buttons">
  <button (mouseup)="button.show = !button.show">
    {{ button?.show ? ('Hide ' + button?.id) : ('Show ' + button?.id) }}
  </button>
  <ng-container *ngIf="button?.show">
    Content for button {{ button?.id }}
  </ng-container>
</div>

工作示例:Stackblitz

文档:*ngFor*ngIf 指令、expression interpolation&lt;ng-container&gt; tag

如果您是 Angular 新手,我还建议您通过官方 tutorial。它介绍了所有这些基础知识。

【讨论】:

  • 这就是我想做的!不过,还有一点是我必须在 ts 文件的 init 部分动态地构造该数组。你能编辑你的答案吗?
  • 这是一个例子。您不必完全按照图中所示使用它。您可以根据需要在 ngOnInit 挂钩中分配值。
  • 是的,我明白这一点,当然会根据我的需要修改他的代码,只是因为我对 Angular 有点陌生,想要一些关于如何在 ngOnInit 内部循环中构建数组的示例。
  • 谢谢,但在循环中。我的意思是动态的。
  • 请提供一个伪示例。你甚至没有显示你需要什么样的按钮。
猜你喜欢
  • 2017-11-02
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 2023-03-17
  • 2020-06-01
相关资源
最近更新 更多