0

I had a question where the (great) answer exemplified how to allow an input box to obtain focus (by requesting change detection to be run in case the component isn't visible at the moment of focus assignment).

Now, I'm faced with a similar problem. I'm setting the value in a field of the component that contains the input box from another component. It works as far I can see the input box being displayed and the read-only label getting hidden. However, the input box isn't focused on.

I'm suspecting that the reason for it is the same, or least similar. However, since I'm setting the value from outside of the component, I can't rely on accessing it as a view child anymore, as I did in the case of the click handler.

What would be an appropriate strategy to approach it?

The usual googling rendered no useful links. I'm not providing a sample because, frankly, I'm not sure how to illustrate the issue other than describe it in words.

onUpdate(config: DataRowConfig) {
  const target = config.cellConfigs.find(a => a.justSubmitted);
  const index = config.cellConfigs.findIndex(a => a.justSubmitted);
  if (target.key === "Enter")
    config.cellConfigs[(column + 1) % 5].editing = true;
}
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Does [this stackblitz](https://stackblitz.com/edit/angular-crqusl?file=src%2Fapp%2Fapp.component.html) correspond to what you are trying to do? – ConnorsFan Apr 16 '19 at 21:41
  • @ConnorsFan Almost or yes (depending on my next googling). In my case, the child is one of several child components created by *\*ngFor*, so I'll have to figure out a way to indexify the ID so my children will be *#ch1*, *#ch2* somehow. I don't expect a collision with your suggestion (hence, *yes* to your question) but I'll know for sure when I've googled indexed IDs (hence, *almost*). Either way, you probably want to post the commend with a short sample to be accepted as an answer. – Konrad Viltersten Apr 17 '19 at 07:09
  • @ConnorsFan Also, is there an alternative approach, based on the principle of encapsulation? I'm thinking about something that reacts to changes in the property and when such change occurs, it'd execute the focusification by invoking it internally, from within. – Konrad Viltersten Apr 17 '19 at 07:11
  • @ConnorsFan After a few hours of trying, I think it's a no-go, regrettably. The reason is that I can't figure out a way to refer to the cells I have that are parts of a row that itself is a part of the component that governs the operation. In your case, you can refer to the component by *#blopp* but in mine, I have a ngFor'ed list of rows and in each a ngFor'ed list of cells. – Konrad Viltersten Apr 17 '19 at 15:05
  • Include the relevant HTML markup in the question. There is probably a way to get what you want with a similar technique. – ConnorsFan Apr 17 '19 at 16:19

0 Answers0