6

I’m writing in hopes of finding out how to trigger angularJS to update its model when performing autocompletion from a browser extensions. I know that some other apps (password managers like LastPass) do this successfully.

I’m building a little Safari extension that will help me auto complete Apple’s (ridiculous) Sandbox User creation form, which has 10 input fields and a couple of selector elements.

So far I have managed to get the input fields using jQuery and to set their value using .val(). However, this does not trigger angularJS to update its model, so even though there is text in the input fields, angular thinks there is no text and fails to validate the form.

The problem could be easily fixed if I can get access to the angular object, but I can’t seem to be able to do that. My injected script returns undefined when I try to access angular. So I’m thinking I’m either retrieving the angular object incorrectly or that I should be solving this a different way.

To note that I have already tried some of the other tricks mentioned around the web: input.trigger('input'); and other such stuff.

Alex
  • 7,432
  • 20
  • 75
  • 118
  • Do you have the field ng-model set? – Jacques ジャック Aug 17 '15 at 18:00
  • 2
    this is very relevant: http://stackoverflow.com/a/11710295/1078450 – wesww Aug 17 '15 at 18:02
  • @Jacques on an element? I am using a form made by someone else, I am not creating my own form... – Alex Aug 17 '15 at 18:07
  • @Andrei I'm a bit confused. If you don't control the form, why are you worried about angular updating the model when the form changes? You can't control what someone else's form does, or what technology they use. The developer of the form should make sure to check for autocomplete. – Jacques ジャック Aug 17 '15 at 18:10
  • @Jacques Right, but because they don't update their form, it won't validate even if it has correct values in each element. It'll think that all fields are empty, even though they have values within. The form validates fine if I manually type in something in the field. I know it is possible to circumvent this "bug" because other extensions such as Last Pass successfully update the fields and the model seems to be correctly updated because the form doesn't complain that there are empty fields – Alex Aug 17 '15 at 18:23

1 Answers1

1

Sounds like you need to fool the form into thinking you've actually typed something into the input, instead of just setting the input's value, which I do not believe causes a focus event. Try using JQuery to focus and then unfocus the inputs. This should simulate the user clicking on the input field and then leaving the input field. If Apple built their form the right way, this should trigger validations/model updates. Hope this helps.

Vinny
  • 449
  • 3
  • 6