I have the following form field that works fine. By this I mean that when I enter, insert, etc. In the field, fooObj.expDate
updated just fine in real time and validation happens. I have a preliminary tag to make this obvious to me.
<pre>{{fooObj.someDate | json}}</pre> <div class="form-group inline-form__input"> <label for="someDate">Some Date</label> <input tabindex="2" type="tel" class="form-control" maxlength="7" placeholder="MM/YY" formControlName="someDate" name="someDate" [(ngModel)]="fooObj.someDate" someDate> </div>
However, I have this someDate
directive in this field. This directive captures past events. It cancels the insert event, does some kind of fancy input formatting, and then does the following:
setTimeout(() => { this.target.value = 'lol fancy date'; }, 3000);
target.value
is my someDate
field. The value becomes updated in the input window (I see that it changes on the screen inside the input). However, fooObj.someDate
NOT updated, and verification does not occur. For instance. setting the target value in the timeout does NOT trigger the same check / object update as entering the key / insert / any other javascript event.
Angular docs are not much useful:
Angular updates bindings (and therefore the screen) only if the application does something in response to asynchronous events, such as keystrokes. This sample code associates the keyup event with number 0, the shortest statement of the template is possible. Although the statement does nothing, it satisfies Angular's requirement for Angular to refresh the screen.
So, how do I initiate an update of a field from a directive in this field?
Edit: I tried to fire the event in the element, as recommended in the comments, using the code found here in my element: How can I fire the onchange event manually?
It works fine, but does not force the field to be updated:
if ("createEvent" in document) { var evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); this.target.dispatchEvent(evt); } else this.target.fireEvent("onchange");
In addition, here I get an idea of ββsynthetic events that do not cause βnormalβ actions like keyDown or something like that (I really hope that I misunderstood or they are wrong in this use case, but this is not the work of trying to re insert event release): https://www.w3.org/TR/clipboard-apis/#clipboard-event-interfaces
Note: Synthetic events do not have a default action. In other words, while the script above will fire the insert event, the data will not actually be inserted into the document.