更新时间:2022-11-19 09:27:35
有不同的方法:
$setViewValue()
更新视图和模型.大多数情况下就足够了.$viewValue
和 $modelValue代码>
ng-model
的内容(例如,指令更改小数位数,同时更新模型),请注入 ngModel: '='
在作用域上设置 scope.ngModel
$setViewValue()
updates the view and the model. Most cases it is enough.$viewValue
and $modelValue
ng-model
(e.g. the directive changes the number of decimals, updating also the model), inject ngModel: '='
on the scope and set scope.ngModel
例如
return {
restrict: 'A',
require: 'ngModel',
scope: {
ngModel: '='
},
link: function (scope, element, attrs, ngModelCtrl) {
function updateView(value) {
ngModelCtrl.$viewValue = value;
ngModelCtrl.$render();
}
function updateModel(value) {
ngModelCtrl.$modelValue = value;
scope.ngModel = value; // overwrites ngModel value
}
...
链接: