angular.js - angularjs双向绑定
世界只因有你
世界只因有你 2017-05-15 16:55:24
0
2
497

input有个ng-model绑定,手动输入能改变这个值,我用jquery改变了一个input的值$('#dd').val('str');
然后scopemodel不变
求救

世界只因有你
世界只因有你

reply all(2)
漂亮男人

If you use angular way to modify and update the value of DOM or variables, no additional operations are required.

But if you break away from angular and use code like jquery that is not within the angular system, you need to notify angular of any modifications and let angular get the latest value to reflect it on the page.

So you can use the following method to notify angular

$scope.$apply(function(){
    jQuery('input').val(newValue);
})
刘奇

The correct way is... don't change the view manually.
Proposal:
1. Try to expose the model, and then modify the model. After the modification, call $scope.$apply(). In addition, in order to avoid various unexpected errors, you can write setTimeout(function(){$scope.$apply()},0) like this.
2. In any case, if you have this need, it must be because of some event, user operation or ajax. Ajax operations can use the $http service provided by Angular. User operations can usually be encapsulated into directives.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template