Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event when div ngIf is rendered

I was looking at issue at: https://stackoverflow.com/a/44737636/973651

I've a div with an ngIf condition, and I'd like to capture an event for when the div gets rendered. Supposedly, you can use the AfterContentInit event. The article I cited above, shows an example, which I've replicated but no luck in getting it to work.

I don't know what I'm doing wrong, but I can't get this to work. I get no errors, but it doesn't work for me.

My directive is defined as:

import { AfterContentInit, EventEmitter, Output, Directive } from '@angular/core';

@Directive({ selector: '[after-if]' })
export class AfterIfDirective implements AfterContentInit {
  @Output('after-if')
  public after: EventEmitter<AfterIfDirective> = new EventEmitter();

  public ngAfterContentInit(): void {
    debugger;
    setTimeout(() => {
      // timeout helps prevent unexpected change errors
      this.after.next(this);
    });
  }
}

In my page component I import.

import { AfterIfDirective } from '../directives/after-if';


@NgModule({
  providers: [],
  imports: [NgModel, BrowserAnimationsModule, HttpClientModule, 
      AfterIfDirective],
  exports: [NgModel, BrowserAnimationsModule, AfterIfDirective]
})

and in the html:

<div *ngIf="chkTearline.checked; else NotTearline" (after-if)="Clicked()">

what am I missing?

Does this not work in Angular 5?

  Clicked() {
    console.log('something got clicked');
  }
like image 683
Les Stockton Avatar asked May 08 '18 20:05

Les Stockton


People also ask

What does ngif mean in angular?

The * syntax means that ngIf is a structural directive, meaning that it affects the structure of the page. When Angular sees the *, the template compiler is going to take the template in its initial form: #N#<div class =" container " *ngIf =" courses.length; else noCourses " >. #N#.

Why are the ngif directives at the top of the template?

Here we have nested the ngIf directives at the top of the component template so that we get all the data needed anywhere in the component upfront. All this nesting doesn't look great visually, but at least it prevents a lot of the code repetition. If it works, why not?

What happens if an element is hidden with ngif?

With ngIf, if an element is hidden then that element does not exist at all in the page. Meaning that if you inspect the page using for example the Chrome Dev Tools, you won't find any HTML element present in the DOM. Instead of it, you will find a strange-looking HTML comment similar to this one, where the ngIf directive was applied:

How to use ngif else in nocourses?

In Angular, we can use the ngIf else syntax, in the following way: .... Besides the courses.length expression, we can also pass to ngIf an else clause, that points to a template reference (the noCourses template in this case).


Video Answer


1 Answers

You can use setter like in example below:

template:

<div #elementToCheck *ngIf="expr"></div>

component:

@ViewChild('elementToCheck') set elementToCheck () {
  // here you get access only when element is rendered (or destroyed)
}
like image 92
Sharikov Vladislav Avatar answered Oct 19 '22 19:10

Sharikov Vladislav