6

Does ng-template have any events when it is visible or active in the scope or is it possible to execute any method in such way to call it from the container?

<ng-template (active/vissible)="callMethod()"> ... </ng-template>

(Or)

<ng-container *ngTemplateOutlet="mytemplate" ></ng-container>
3
  • Does the template only become visible at some point, or does it also go from visible to not visible?
    – ConnorsFan
    Apr 19, 2018 at 17:49
  • it only gets visible at some point It is being used within an ng-tabset Apr 19, 2018 at 17:52
  • In that case, the answer given by popStar should work.
    – ConnorsFan
    Apr 19, 2018 at 17:56

2 Answers 2

3

One option could be (valid for any DOM object) is via ViewChildren's QueryList's changes. You mark any DOM element (or use it's type) - <div #myEl></div>

Assign it:

@ViewChildren('myEl') myEl: QueryList<any>;

And subscribe for changes in ngAfterViewInit (as earlier AFAIK its not yet created):

ngAfterViewInit() {
  this.myEl.changes.subscribe(_ => console.log(_));
}
3
  • 1
    I am not familiar with ngTabset, but this should work with any DOM element Apr 19, 2018 at 18:05
  • isn't there any other way to implement it other than a lifecycle hook Apr 19, 2018 at 19:32
  • Pretty sure there are other ways. Why you dont want lifecycle hook? Apr 20, 2018 at 4:19
-1

You can use

public mycondition: boolean = true;   ///according to need make it true or false

<ng-template *ngIf="mycondition"> ... </ng-template>
3
  • can I call a method from it ? @Lavkush Gupta I need to call a method when the template is vissible Apr 19, 2018 at 17:24
  • Yes Of cause you can call a method Apr 19, 2018 at 17:27
  • I do not want to make the view visible/hide with *ngIf I need it to notify to the component that it is active or vissible like an event Apr 19, 2018 at 17:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.