【发布时间】:2016-07-16 08:44:31
【问题描述】:
我有一些 ng-model 输入元素,它们可以获取更新的非 angularJS 函数,例如 jquery,或者有时通过纯 javascript DOM api。 html 输入元素上的值会发生变化,但模型范围变量不会更新。 有什么方法可以强制 Angular 处理这些更新
app.js
超时 5 秒后,值 1 更改为 999。这反映在 html 中,但不在 $scope.val 中
angular.module('inputExample', [])
.controller('ExampleController', ['$scope', '$timeout',function($scope,$timeout) {
$scope.val = '1';
$timeout(function(){
document.getElementById("myid").value = 999;
},5000)
}]);
html
<form name="testForm" ng-controller="ExampleController">
<input id="myid" ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input"
aria-describedby="inputDescription" />
<div>
{{val}}
</div>
</form>
我也将代码保存在 plunker http://plnkr.co/edit/4OSW2ENIHJPUph9NANVI?p=preview
【问题讨论】:
-
您可能希望在更改值后从输入元素触发“输入”事件。不确定,但可能会使用最新值更新角度。
标签: javascript angularjs angularjs-scope angular-ngmodel angularjs-ng-model