Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

angularjs - How to trigger ng-change from jquery?

I have a select tag as follows:

<select ng-change="doSomething()" ng-model="myModel"> </select>

and I'm using a jQueryUI control for it (combobox), the event "change" triggered from jQuery doesn't trigger doSomething().

Do you know how to trigger ng-change from outside angular?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The question is not how to run some code, but rather how to trigger the change event. I found an angular-friendly way to do this. I have a jQuery UI slider that changes the value of another input which has the ng-change on it. The secret is that once you turn it into an angular.element you now have access to a method called triggerHandler. From what I've seen, using this would not be considered bad practice:

angular.element(document.getElementById('test')).triggerHandler('change');

tymeJV's answer did angular.element("#test") directly without using jQuery, but I got a console error and broken functionality that way. This version works without any dependencies.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...