如何通知AngularJS表单已由jQuery外部填充? [英] How to notify AngularJS that a form has been filled externally by jQuery?

查看:63
本文介绍了如何通知AngularJS表单已由jQuery外部填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AngularJS中有一个页面,其中包含一些包含某些字段的表单。在加载该页面时,我想要一个书签,在点击时根据配置文件中的数据填充字段。正在使用一个JS文件,它使用jquery来填充表单中的数据

I have a page in AngularJS which has a form containing some fields. Upon loading that page, I want a bookmark which when clicked to fill the fields based on data in a configuration file. A JS file is being used which uses jquery to fill the data in the form

当我执行 $(#id)。val( 'abcd'),该字段填充了文本 abcd ,但angularjs认为表单仍然无效。类 ng-pristine ng-invalid 仍然保留在元素上。我尝试删除使用jquery的 removeClass()并使用<$ c $添加 ng-dirty ng-valid 类c> addClass()但是表单状态仍然无效。

When I do $("#id").val('abcd'), the field gets populated with text abcd, but angularjs thinks the form is still invalid.The classes ng-pristine ng-invalid still remain on the element. I tried removing the using jquery's removeClass() and adding the ng-dirty ng-valid classes using addClass() but still the state of the form remains as invalid.

如何让angularjs知道表单字段已经存在填充并且表单有效吗?

How do I let angularjs know that the form fields have been populated and the form is valid?

推荐答案

ng-model 控制器收听'更改'事件。

<input id="id" ng-model="$ctrl.text" />

从jQuery触发它:

To trigger it from jQuery:

$("#id").val("abcd").trigger("change");

这将设置输入的值并触发 ngModelController 更新AngularJS框架中的模型。

This will set the value of the input and trigger the ngModelController to update the model in the AngularJS framework.

$(function() {
  $("#bt").on("click", function() {
    $("#id").val("abcd").trigger("change");
  });
})

<script src="//unpkg.com/jquery"></script>
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app>
    <input id="id" ng-model="$ctrl.text" /><br>
    text={{$ctrl.text}}<br>
    <button id="bt">Click me</button>
</body>

在非常重要> AngularJS库。

It is important to load the jQuery library before the AngularJS library.

来自文档:


使用 jQuery ,只需确保它在 angular.js 文件之前加载。

To use jQuery, simply ensure it is loaded before the angular.js file.

有关详细信息,请参阅 AngularJS angular .element API参考

For more information, see AngularJS angular.element API Reference.

这篇关于如何通知AngularJS表单已由jQuery外部填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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