【问题标题】:Angular - custom popup htmlAngular - 自定义弹出 html
【发布时间】:2021-04-07 15:24:49
【问题描述】:

我正在尝试实现一些我必须在单击按钮时打开一个弹出窗口的东西,可以有多个按钮具有自己的叠加弹出窗口,并且在单击弹出窗口外部时它们应该关闭。现在我正在使用 TemplateRef (在我的情况下为#toggleButton) 打开它们,但它只在一个按钮上工作,即第一个按钮,它同时打开所有弹出窗口。任何帮助将不胜感激。

我正在附加 stackblitz 链接,https://stackblitz.com/edit/angular-d8tnwg?file=src/app/app.component.ts

HTML:

<ng-container>
            <tr>
                <td class="custom-td" style="overflow:visible;">
                    <button #toggleButton class="icon-info" (click)="showTooltip()"></button>
                    <div #menu class="overlay-text" [ngClass]="status ? 'open' : ''">
                        <p>This is a placeholder text and will be copied
                            if you click on button!</p>
                        <div class="form-group">
                            <input type="text">
                            <button  class="icon-filecopy"></button>
                        </div>

                    </div>
                </td>
            </tr>
            <tr>
                <td class="custom-td" style="overflow:visible;">
                    <button #toggleButton class="icon-info" (click)="showTooltip()"></button>
                    <div #menu class="overlay-text" style="background-color: yellow" [ngClass]="status ? 'open' : ''">
                        <p>This is a placeholder text and will be copied
                            if you click on button!</p>
                        <div class="form-group">
                            <input type="text">
                            <button  class="icon-filecopy"></button>
                        </div>

                    </div>
                </td>
            </tr>
            <tr>
                <td class="custom-td" style="overflow:visible;">
                    <button #toggleButton class="icon-info" (click)="showTooltip()"></button>
                    <div #menu class="overlay-text" style="background-color: red" [ngClass]="status ? 'open' : ''">
                        <p>This is a placeholder text and will be copied
                            if you click on button!</p>
                        <div class="form-group">
                            <input type="text">
                            <button class="icon-filecopy"></button>
                        </div>

                    </div>
                </td>
            </tr>
        </ng-container>

TS:

status: boolean = false;
  @ViewChild("toggleButton") toggleButton: QueryList<ElementRef>;
  @ViewChild("menu") menu: QueryList<ElementRef>;

  constructor(private renderer: Renderer2) {
    this.renderer.listen("window", "click", (e: Event) => {
      console.log("click outside");
      if (
        e.target !== this.toggleButton.nativeElement &&
        e.target !== this.menu.nativeElement
      ) {
        this.status = false;
      }
    });
  }

  showTooltip() {
    this.status = !this.status;
  }
}

【问题讨论】:

  • 好吧,首先,我不会在构造函数中添加任何内容,您应该将所有内容移至 ngOnInit angular.io/api/core/OnInit。然后,如果您希望每个按钮打开不同的弹出窗口,则需要使它们唯一,如果您使用相同的标志来启用/禁用所有人,它将影响所有人。如果您不想创建单独的方法,请发送一个 ID,并仅切换受影响的...我希望这是有道理的
  • @rmjoia 如何使用唯一 ID 打开?抱歉,我问的是愚蠢的问题,这里相对较新
  • 没有“愚蠢的问题”提问是健康的,学习的第一步是承认你不知道 :) 我发布了一个可能解决方案的答案,我希望它可以帮助你掌握窍门它并到达那里..最终你可能想检查这个免费的很棒的资源。 thinkster.io/topics/fundamentals-of-angular
  • 我在点击外部时添加了第二次 StackBlitz 关闭,虽然可能需要一些调整.. 但我不能花更多时间在这上面,我认为这两个示例都提供了关于如何根据我的理解实现你正在寻找的东西

标签: angular typescript dom binding click


【解决方案1】:

只有第一个按钮有效,因为您的 id 不是唯一的,解决方法是:@ViewChild(Button) toggleButton: QueryList&lt;ElementRef&gt; 但正如 rmjoia 评论的那样 - 实现您想要的正确方法是为每个按钮制作唯一的 ID 和唯一的工具提示切换

【讨论】:

    【解决方案2】:

    以下是一种可能的解决方案,我并不是说这是最好的或唯一的解决方案,在编程中,通常有不止一种解决方案,并且每种解决方案都有其权衡。

    我做了功能切换,但如果你愿意,你可以设置一个为真,弹出按钮设置为假...天空甚至不是极限;)

    StackBlitz example forked from yours

    app.component.ts

    import { Component } from "@angular/core";
    
    @Component({
      selector: "my-app",
      templateUrl: "./app.component.html",
      styleUrls: ["./app.component.css"]
    })
    export class AppComponent {
      isTooltip1Visible = false;
      isTooltip2Visible = false;
      isTooltip3Visible = false;
    
      constructor() {}
    
      toggleTooltip(tooltipId) {
        switch (tooltipId) {
          case 1:
            this.isTooltip1Visible = !this.isTooltip1Visible;
            break;
          case 2:
            this.isTooltip2Visible = !this.isTooltip2Visible;
            break;
          case 3:
            this.isTooltip3Visible = !this.isTooltip3Visible;
            break;
          default:
            console.log("Invalid tooltip Id");
            break;
        }
      }
    }
    

    app.component.html

    <table appClickOutside (clickOutside)="closeDropdown()">
        <tbody>
            <ng-container>
                <tr>
                    <td class="custom-td" style="overflow:visible;">
                        <button #toggleButton class="icon-info" (click)="toggleTooltip(1)"></button>
                        <div #menu class="overlay-text" [ngClass]="isTooltip1Visible ? 'open' : ''">
                            <p>This is a placeholder text and will be copied
                                if you click on button!</p>
                            <div class="form-group">
                                <input type="text">
                                <button (click)="toggleTooltip(1)" class="icon-filecopy"></button>
                            </div>
    
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="custom-td" style="overflow:visible;">
                        <button #toggleButton class="icon-info" (click)="toggleTooltip(2)"></button>
                        <div #menu class="overlay-text" style="background-color: yellow"
                            [ngClass]="isTooltip2Visible ? 'open' : ''">
                            <p>This is a placeholder text and will be copied
                                if you click on button!</p>
                            <div class="form-group">
                                <input type="text">
                                <button (click)="toggleTooltip(2)" class="icon-filecopy"></button>
                            </div>
    
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="custom-td" style="overflow:visible;">
                        <button #toggleButton class="icon-info" (click)="toggleTooltip(3)"></button>
                        <div #menu class="overlay-text" style="background-color: red"
                            [ngClass]="isTooltip3Visible ? 'open' : ''">
                            <p>This is a placeholder text and will be copied
                                if you click on button!</p>
                            <div class="form-group">
                                <input type="text">
                                <button (click)="toggleTooltip(3)" class="icon-filecopy"></button>
                            </div>
    
                        </div>
                    </td>
                </tr>
            </ng-container>
        </tbody>
    </table>
    

    【讨论】:

    • 我想保持动态,可以有很多按钮和弹出窗口
    • 即使您将其设为动态,该示例也应该足以让您到达那里。如果您有一个项目列表,然后动态填充按钮和弹出窗口,您可以在项目上使用 isVisible 属性,并使用项目 ID 切换它。根据您提供的代码,这是一个可能的解决方案...
    • 我创建了另一个 Stakblitz 只是为了展示...如何动态完成stackblitz.com/edit/angular-be9sr1 您的问题是关于如何为该静态代码执行此操作,我认为我的回答是有效的,如果您'正在这个例子中寻找类似的东西,你可以看到相似之处以及如何使用我的答案来获得这个结果,或者,编辑你的问题,如果这是你正在寻找的,我将替换代码
    猜你喜欢
    • 2015-07-15
    • 1970-01-01
    • 2019-05-11
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 2015-07-15
    相关资源
    最近更新 更多