我不明白为什么当我调用 $setViewValue 函数时表达式会更新而 ng 模型值不会更新。
这是骗子:
http://plnkr.co/edit/2xdsujEsqDVVXaSPNqYE?p=preview
代码:
<body ng-controller="MainCtrl">
<input type="text" ng-model="user.name" set-value>
{{user.name}}
<input type="button" value="click" ng-click="change()">
</body>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.user = {name:'name'};
})
.directive('setValue', function() {
return {
restrict:'A',
require:'ngModel',
link: function(scope,ele, attr, ctrl) {
scope.change = function() {
ctrl.$setViewValue('text');
}
}
}
})
最佳答案
尝试在执行ctrl.$setViewValue('text')
之后立即添加ctrl.$render()
。这将导致 UI 使用最新的模型更改进行更新。
关于javascript - Angularjs $setViewValue 不影响 ngModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828780/