Trigger function if * ngIf is executed? angular 2

I put the *ngIf condition on one of my buttons, which appears if certain fields are checked, it calls the geolocation() function when clicked. Instead, I want to automatically run this function as soon as ngIf is ngIf . I remember using ng-init for this with angular 1.x, but now it does not work, and *ngInit does not work.

 <button *ngIf="!venueNameInput.control.valid && !address1Input.control.valid" (click)="geolocation()">Test Dojo Geolocation</button> 
+5
source share
1 answer

Implement your own discovery lifecycle binding method, with some component state:

 geoCalled = false; ... ngDoCheck() { if(!this.geoCalled && this.shouldCallGeo()) { this.geolocation(); this.geoCalled = true; } } shouldCallGeo() { return !this.venueNameInput.control.valid && !this.address1Input.control.valid; } shouldDisplay() { return this.shouldCallGeo(); } 

Update view:

 <button *ngIf="shouldDisplay()">Test Dojo Geolocation</button> 
+1
source

Source: https://habr.com/ru/post/1240555/


All Articles