【问题标题】:click event happening on all divs instead of one at a time - Angular5单击事件发生在所有 div 上,而不是一次发生一个 - Angular5
【发布时间】:2018-09-20 13:12:50
【问题描述】:

我对使用 Angular 非常陌生,并且遇到了一个问题!我在 Angular 5 中制作的常见问题解答页面上创建了一个点击事件。我遇到的问题是,当我单击一个常见问题解答时,它们都会打开,而不仅仅是目标问题。

这是我的ts:

public show_content = true;
  showContent () {
    this.show_content = !this.show_content;
  }

还有我的html:

<h4 class="accordion-toggle" (click)="showContent()">When is the app ready for download?</h4>
      <hr>
            <div class="accordion-content" *ngIf="!show_content">
                <p>
                    Cras malesuada ultrices augue molestie risus.
                </p>
            </div>
      <h4 class="accordion-toggle" (click)="showContent()">When is the app ready for download?</h4>
      <hr>
            <div class="accordion-content" *ngIf="!show_content">
                <p>
                    Cras malesuada ultrices augue molestie risus.
                </p>
            </div>

我知道我在两者上都使用了相同的 *ngIf,这就是发生这种情况的原因。我要问的问题是:有没有办法在不为每个常见问题解答创建多个变量的情况下做到这一点?我知道创建一个像这样的新函数:

showContent2 () {
    this.show_content2 = !this.show_content2;

  }

然后在我的 html 中有这个:

  <h4 class="accordion-toggle" (click)="showContent2()">When is the app ready for download?</h4>
      <hr>
            <div class="accordion-content" *ngIf="!show_content2">

这确实可以解决问题,但是我想知道是否有更好的方法可以做到这一点,因为这似乎很冗长。

【问题讨论】:

    标签: javascript html typescript angular5


    【解决方案1】:

    一种方法是,您可以首先将每个常见问题解答重构为一个常见问题解答组件。这样你就可以隔离每个人了

    在 /src/app/my-faq/my-faq.component.ts

    import { Component, Input } from '@angular/core';
    
    @Component({
      selector: 'my-faq',
      templateUrl: './my-faq.component.html',
      styleUrls: ['./my-faq.component.scss']
    })
    export class MyFaqComponent {
      @Input() title: string;
      @Input() content: string;
      show_content = false;
    
      constructor() {}
    
      showContent() {
        this.show_content = !this.show_content;
      }
    }
    

    在/src/app/my-faq/my-faq.component.html:

    <h4 class="accordion-toggle" (click)="showContent()">{{ title }}</h4> 
    <hr>
    <div class="accordion-content" *ngIf="!show_content">
      <p>{{ content }}</p>
    </div>
    

    在 /src/app/my-faq/my-faq.component.scss

    .accordion-toggle {
      // your css here
    }
    .accordion-content {
      // your css here
    }
    

    在 /src/app/app.module.ts 中添加:

    ...
    import { MyFaqComponent } from './my-faq/my-faq.component.ts'
    ...
    
    @NgModule({
      declarations: [
        ...,
        MyFaqComponent
      ]
    ...
    

    在您的应用程序中声明组件后,在您的 html 中,现在您可以这样做:

    <my-faq [title]="'When is the app ready for download?" [content]="'Cras malesuada ultrices augue molestie risus.'"></my-faq>
    <my-faq [title]="'When is the app ready for download?" [content]="'Cras malesuada ultrices augue molestie risus.'"></my-faq>
    

    【讨论】:

    • 你的意思是每个常见问题都有一个全新的组件吗?还是只有一个模板? (很抱歉,对此很陌生)
    • 别担心!没有。我的意思是只有一个可重用的组件。我为您的示例添加了一个用例。
    • 如何将新组件导入到我当前的组件中?
    • 我假设你有一个应用模块文件(通常是 app.module.ts)。在那里,您可以声明新的 my-faq 组件,它可以在您的应用程序的其他任何地方使用,也可以在其他组件的模板中使用,而无需导入任何内容。
    • 啊,当然!这样行得通,现在唯一不工作的是我的课程不再工作?有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 2023-03-09
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多