I have a form element with an ng-change handler. I want to programmatically trigger the handler, but without direct inspection of scope. I want to do this because I'm writing a Chrome extension so I can't easily access '$scope' to get the handler.
I have tried the obvious choice of
$(element).triggerHandler('change')
but that doesn't seem to work. See example here: https://plnkr.co/edit/iaz7trxVT09XWBktGhE9?p=preview (in this example I'm logging some lines to console when the change handler runs, but clicking the button doesn't log those lines).
I tried a few other methods found in various threads here, such as trigger() or manual construction of event and fire with dispatchEvent but to no avail. I don't quite understand why the event handler isn't triggering. Can anyone help?
ng-<event>directives,ng-changeis not bound to the underlying element'schangeevent. It is bound tong-model's$viewChangeListeners, so there is no way to invoke it manually, unless you have access to tge scope. Why are you usingng-change? Wouldng-inputwork instead?scope = angular.element($(element)).scope(), so there is no need to triggerchangeangular.element(element).scope()as it's pure JS and the data should be stored in DOM anyway).