如何从 jquery 触发 ng-change? [英] How to trigger ng-change from jquery?

查看:30
本文介绍了如何从 jquery 触发 ng-change?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 select 标签,如下所示:

I have a select tag as follows:

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

我正在使用 jQueryUI 控件(组合框),从 jQuery 触发的事件更改"不会触发 doSomething().

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

你知道如何从外部角度触发ng-change吗?

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

推荐答案

问题不是如何运行某些代码,而是如何触发更改事件.我找到了一种角度友好的方法来做到这一点.我有一个 jQuery UI 滑块,它更改了另一个带有 ng-change 的输入的值.秘诀在于,一旦将其转换为 angular.element,您就可以访问名为 triggerHandler 的方法.从我所见,使用它不会被认为是不好的做法:

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 的回答直接做了 angular.element("#test") 而不使用 jQuery,但我遇到了控制台错误和损坏的功能.此版本无需任何依赖即可运行.

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.

这篇关于如何从 jquery 触发 ng-change?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆