2

我有一些ng-model输入元素获取更新的非angularJS函数,如jQuery或有时由纯JavaScript DOM API。 html输入元素上的值发生变化,但模型范围变量没有更新。 是否有这反映在HTML,但不是在$范围内的任何办法强迫的角度来处理这些更新更新角度ng模型中的HTML输入值更改

app.js
超时5secs后,值1改为999。 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

+0

您可能要触发“输入”事件从更改值后的输入元素。不确定,但可能会随角度更新最新值。 – HankScorpio

回答

0

你可以做到这一点,

$timeout(function(){ 
     document.getElementById("myid").value = 999; 
     angular.element(document.getElementById("myid")).triggerHandler('change'); 
    },5000) 

然而,更好的方法是直接更新的范围,

$timeout(function(){ 
      angular.element(document.getElementById("myid")).scope().val = 999; 
     },5000) 
1

不触发更新999某种输入的变化,你需要做的是:

angular.module('inputExample', []) 
    .controller('ExampleController', ['$scope', '$timeout',function($scope,$timeout) { 
    $scope.val = '1'; 
    $timeout(function(){ 
     $scope.val = 999; 

    },5000) 
    }]); 

你不需要使用像document.getElement(S)什么...角度。 只需设置一个新值上$scope.val

以下是更正Plunker:http://plnkr.co/edit/fWxMeNcJFtQreMKZaB5H?p=preview

0

您可以手动设置的表单元素,这将迫使更新viewValue。

$timeout(function(){ 
    var ele = document.getElementById("myid"); 
    ele.value = 999; 

    $scope.testForm.anim.$setViewValue(ele.value); 
},5000); 

查看最新的plunkr

1

更新$scope而不是DOM元素。

$scope.val = 999;