简体   繁体   English

Angular 6显示或隐藏列表项

[英]Angular 6 show or hide list items

I have simple angular app which displays list items, I want when first list item is clicked list description(card) is displayed , when second list item is clicked and description is displayed the first list description(card) should be hidden , 我有一个简单的角度应用程序,用于显示列表项,我想在单击第一个列表项时显示列表描述(卡),在单击第二个列表项并显示描述时要隐藏第一个列表描述(卡),

Here is stackblitz for reference: https://stackblitz.com/edit/bootstrap-nabar-cidoez?file=src/app/app.component.html 以下是stackblitz供参考: https ://stackblitz.com/edit/bootstrap-nabar-cidoez?file = src/app/app.component.html

html: HTML:

<div class="why-choose-us__description">
      <ul class="why-choose-us__list-top">
        <li class="why-choose-us__leader" 
        (click)="toggleCard()"
        style="background-image: url('/assets/images/solidne-fundamenty.png')">
          <h4>Inspiring Leaders1</h4>
          <div class="why-choose-us__card card" *ngIf="showCard">
            <p>Thanks to belonging to an international organization, which operates since 1988 and has a huge knowledge base,
              ASTEK Poland enjoys its stable position on the market.</p>
            <div class="close-icon"></div>
          </div>
        </li>
          <li class="why-choose-us__leader" 
        (click)="toggleCard()"
        style="background-image: url('/assets/images/solidne-fundamenty.png')">
          <h4>Inspiring Leaders2</h4>
          <div class="why-choose-us__card card" *ngIf="showCard">
            <p>Thanks to belonging to an international organization, which operates since 1988 and has a huge knowledge base,
              ASTEK Poland enjoys its stable position on the market.</p>
            <div class="close-icon"></div>
          </div>
        </li>
          <li class="why-choose-us__leader" 
        (click)="toggleCard()"
        style="background-image: url('/assets/images/solidne-fundamenty.png')">
          <h4>Inspiring Leaders3</h4>
          <div class="why-choose-us__card card" *ngIf="showCard">
            <p>Thanks to belonging to an international organization, which operates since 1988 and has a huge knowledge base,
              ASTEK Poland enjoys its stable position on the market.</p>
            <div class="close-icon"></div>
          </div>
        </li>


      </ul>
 </div> 

component.ts component.ts

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
    showCard = false;

  toggleCard() {
    this.showCard = !this.showCard;
  }
}

Now when I click one of my list all card descriptions from other list also is displayed . 现在,当我单击列表之一时,也会显示其他列表中的所有卡说明。

what am I missing in my codes? 我的代码中缺少什么? any help will be apreciated, thanks 任何帮助将不胜感激,谢谢

First I will prefer to use an array of leaders, with a boolean field: 首先,我更喜欢使用带有布尔字段的一系列领导者:

inspiringLeaders = [
    {
      name: 'Inspiring leaders 1',
      text: 'Thanks to belonging to an international organization, which operates since 1988 and has a huge knowledge base, ASTEK Poland enjoys its stable position on the market.',
      shown: false
    },
    {
      name: 'Inspiring leaders 2',
      text: 'Thanks to belonging to an international organization, which operates since 1988 and has a huge knowledge base, ASTEK Poland enjoys its stable position on the market.',
      shown: false
    },
    {
      name: 'Inspiring leaders 3',
      text: 'Thanks to belonging to an international organization, which operates since 1988 and has a huge knowledge base, ASTEK Poland enjoys its stable position on the market.',
      shown: false
    }
  ];

toggleCard(leader: { name: string, text: string, shown: boolean }) {
    this.inspiringLeaders.map((l) => {
      if (l.name === leader.name) {
        l.shown = !l.shown;
      } else {
        l.shown = false;
      }
    });
}

and use a ngFor loop in the .html : 并在.html中使用ngFor循环:

<div class="why-choose-us__description">
    <ul class="why-choose-us__list-top">
        <li class="why-choose-us__leader" (click)="toggleCard(leader)" style="background-image: url('/assets/images/solidne-fundamenty.png')"
         *ngFor="let leader of inspiringLeaders">
            <h4>{{leader.name}}</h4>
            <div class="why-choose-us__card card" *ngIf="leader.shown">
                <p>{{leader.text}}</p>
                <div class="close-icon"></div>
            </div>
        </li>
    </ul>
</div>

Updated stackblitz 更新了stackblitz

EDIT 编辑
Show only one text at a time. 一次只显示一个文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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