要打印ngx-bootstrap函数的Typescript

时间:2018-03-06 08:59:31

标签: angular typescript frontend angular5 ngx-bootstrap

我正在尝试将我的打字稿与ngx-bootstrap中的隐藏方法相关联,在我的project中我正在运行多个函数,当用户单击x中的x按钮时,它将全部关闭poppos,typescript中的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>

这是来自plunker的代码,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!
  }

1 个答案:

答案 0 :(得分:2)

您可以在班级中访问模板参考变量。

您的HTML

<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
  }


}

工作plunkr