与jQuery值更新角模型 [英] Update Angular model with jQuery value

查看:172
本文介绍了与jQuery值更新角模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有它连接我的模型,当用户手动类型到它的功能是建立在变化称为输入字段 - 这个工作正常,该模型反映预期在控制台日志

Hi I have an input field which is attached my model and when the user manually types into it a function is called upon change - this works fine and the model is reflecting as expected in the console log.

<input type="text" class="rating" ng-model="review.Rating" ng-change="change()" />

这个问题我现在是,我有第二个jQuery的功能,它可以由自动填充输入字段在页面上的用户调用,但似乎并不只触发模型​​的更新视觉输入字段的值。

The issue I now have is that I have a second jQuery function which can be called by the User on the page which auto-populates the input field, but that doesn't seem to trigger an update of the model only the visual value of the input field.

因此​​,我已添加到第二jQuery的以下调用来触发更新(根据我的研究,这应该是正确的)

I have therefore added to that second jQuery the following call to trigger an update (based on my research this should be correct)

$(".rating").trigger('input');

这似乎是工作在Firefox和Chrome罚款,我有它反映然而,在IE浏览器的控制台日志模型(我有11 - 不确定的旧的浏览器)触发输入似乎并没有在更新模型工作。

This seems to work fine in Firefox and Chrome as I have it reflecting the model in the console log however in IE (I have 11 - unsure of older browsers) the trigger input doesn't seem to work on updating the model.

希望能对你有所帮助。

推荐答案

更新的答案:

我认为这是行不通的,因为角度可能不知道的变化。尝试调用$ $范围摘要()在更新模型或加$范围内的功能之后$申请(函数(){$ ...});

I think that it does not work, because Angular is probably not aware of the change. Try to call $scope.$digest() after you update the model or add the function inside of $scope.$apply(function() { $.....});

而不是使用jQuery的,你可以在你的控制器,将值的NG-模型添加功能。

Instead of using jQuery, you can just add a function in your controller which assigns the value to the ng-model.

例如

<input type="text" ng-model="data.input" />
<button ng-click="autoFill()">Click me</button>

和在你的控制器

$scope.autoFill = function() {
    $scope.data.input = "Hello World!";
};

这篇关于与jQuery值更新角模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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