I have this tiny sample project https://stackblitz.com/edit/angular-material-input-sample-nonw
What I am trying to do is to have user enter a piece of single line text by clicking on a placeholder say [Enter Image Caption] as shown, the user clicks there, and a MatInput element is shown where the user can enter text. When user press Enter, text is saved and show instead of initial placeholder, also, if user goes to another element that text entered gets saved.
I an using ngClass to hide and show placeholder text or MatInput control depending on editMode property of component.
Problems I have:
- When I click on placeholder text, I have to click again, so this.inputField.nativeElement.focus(); is not doing what's supposed to do.
- When you click Edit button that's a simple call to this.inputField.nativeElement.focus(); and as you can see it only works when MatInput is on screen (not hidden), i.e. it saves the user one extra click to actually start typing text on input field.
The question becomes: How do I guarantee that when I stop hiding an element (show via toggle variable in [ngClass]), setting the focus on that element would actually work as expected?. Or is there anyway to guarantee that an element is actually shown on screen so that element.focus can work.