انگولار - تایپاسکریپت برای فراخوانی تابع ngx-bootstrap


من در حال تلاش هستم تا تایپاسکریپت خود را با متد hide از ngx-bootstrap وصل کنم، در پروژهام من چندین تابع را اجرا میکنم و همه آنها وقتی کاربر روی دکمه x در پاپآور کلیک میکند به طور همزمان بسته میشوند، myfunction() در تایپاسکریپت اجرا خواهد شد و متد hide پاپآور pop2.hide() را فعال میکند.

<div>
  <ng-template #popoverContent3>
    <div style="font-size:18px; font-family:'Times New Roman'; font-weight:bold;">
      <p>Fun with ngx-bootstrap
              <button type="button" (click)='pop2.hide()' class="close" aria-label="Close">
      <span aria-hidden="true">&times;</span>
      </button>
      </p>
    </div>
    <div>
     <p>Trying to make typescript call the function pop2.hide()</p>  
    </div>
  </ng-template>
  <br><br><br><br><br><br><br>
  <a  [popover]="popoverContent3" #pop2="bs-popover" (click)="isOpen = !isOpen" [outsideClick]="false"  placement="right">
 Make typescript call the function pop2.hide()
    </a>
</div>

این کد از پلنکر است، دکمه x.

<button type="button" (click)='pop2.hide()' class="close" aria-label="Close">
      <span aria-hidden="true">&times;</span>
</button>

و از تایپاسکریپت خودم، نیاز دارم متد من کار کند.

  myfunction(){
    pop2.hide();//needs to work!
  }

پاسخ شماره ۱

شما میتوانید به متغیر مرجع قالب در کلاس خود دسترسی داشته باشید.

اچتیامال شما

<body>
  <div>
    <ng-template #popoverContent3>
      <div style="font-size:18px; font-family:'Times New Roman'; font-weight:bold;">
        <p>Fun with ngx-bootstrap
          <button type="button" (click)='myfunction()' class="close" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </p>
      </div>
      <div>
        <p>Trying to make typescript call the function pop2.hide()</p>
      </div>
    </ng-template>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <a [popover]="popoverContent3" #pop2="bs-popover" (click)="isOpen = !isOpen" [outsideClick]="false" placement="right">
 Make typescript call the function pop2.hide()
    </a>
  </div>


</body>

ببینید، من myFunction را به عنوان مسئول کلیک روی دکمه بستن اضافه کرده ام. و در مؤلفه

import { Component, ViewChildren, QueryList, AfterViewInit,ViewChild } from '@angular/core';
import { PopoverDirective } from 'ngx-bootstrap';
@Component({
  selector: 'ngx-app',
  templateUrl: 'app.component.html',
  styles:[`



  ]

  `
})
export class AppComponent implements AfterViewInit{
  @ViewChildren(PopoverDirective) popovers: QueryList<PopoverDirective>;
    @ViewChild('pop2') pop2: ElementRef; //this is change


  ngAfterViewInit() {
    this.popovers.forEach((popover: PopoverDirective) => {
      popover.onShown.subscribe(() => {
        this.popovers
        .filter(p => p !== popover)
        .forEach(p => p.hide());
      });
    });
  }
  myfunction(){
    this.pop2.hide();//working
  }


}

پلنکر در حال کار