2

I'm trying to link my typescript with my the hide method from ngx-bootstrap, in my project I am running multiple functions and it will close all together when the user click the x button in the popover, myfunction() in the typescript will run and trigger the hide method 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>

This is the code from plunker, the x button.

<button type="button" (click)='pop2.hide()' class="close" aria-label="Close">
      <span aria-hidden="true">&times;</span>
</button>
And from my typescript, I need my method to work.

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

2
  • Did you manage to make this work? I'm having the same problem right now. Commented Jan 3, 2019 at 8:37
  • @bananaCute yes can read the comments below Commented Jan 3, 2019 at 8:56

1 Answer 1

2

You can access template reference variable in your class.

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

See I have added myFunction as click handler on close button. and in component

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
  }


}

working plunkr

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.