I'm placing an *ngIf
condition on one of my buttons that gets displayed if certain fields validate, it calls the geolocation()
function on click. Instead I want to automatically trigger this function as soon as ngIf
is satisfied. I remember using ng-init
for this with angular 1.x, but it doesn't seem to work now nor does *ngInit
.
<button *ngIf="!venueNameInput.control.valid && !address1Input.control.valid" (click)="geolocation()">Test Dojo Geolocation</button>
Implement your own change detection lifecycle hook 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 your view:
<button *ngIf="shouldDisplay()">Test Dojo Geolocation</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With