javascript 从指令更改 ngModel 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25227320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the ngModel value from a directive
提问by Naor
I am using AngularJS and I created a directive that requires 'ngModel':
我正在使用 AngularJS,我创建了一个需要“ngModel”的指令:
'use strict';
angular.module('spot.im.embed').directive('sayBox', ['$sce', '$timeout', '$parse',
function($sce, $timeout, $parse) {
return {
restrict: 'EA',
require: 'ngModel',
scope: {
},
link: function(scope, iElement, iAttrs, ngModel) {
ngModel.$viewValue = 'adasd';
}
}
}
]);
For reasons I don't know, the ng-model changes doesn't impact the view. Why is that? Is this the right way to change the ngModel value from a directive?
由于我不知道的原因,ng-model 更改不会影响视图。这是为什么?这是从指令更改 ngModel 值的正确方法吗?
回答by Krzysztof Safjanowski
$viewValueis property, $setViewValueis method that you are probably looking for
$viewValue是属性,$setViewValue是您可能正在寻找的方法
link: function (scope, iElement, iAttrs, ngModel) {
ngModel.$setViewValue('adasd');
ngModel.$render(); // depends – if you want update input value or only model value in the scope
}
$setViewValue(value, trigger);
$setViewValue(value, trigger);
This method should be called when an input directive want to change the view value; typically, this is done from within a DOM event handler.
当输入指令要更改视图值时,应调用此方法;通常,这是在 DOM 事件处理程序中完成的。
documentation: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
文档:https: //docs.angularjs.org/api/ng/type/ngModel.NgModelController
回答by Bogdan Savluk
It does not impact view, because $viewValue
is updated on next digest cycle before it is displayed.
它不会影响视图,因为$viewValue
在显示之前会在下一个摘要周期更新。
If you need to change view value not touching model value - you can do this using $formatters
from NgModelController, and $parsers
to update model value from view value (ngModel controller documentation).
如果您需要更改不接触模型值的视图值 - 您可以使用$formatters
来自 NgModelController来执行此操作,并$parsers
从视图值更新模型值(ngModel 控制器文档)。
link: function(scope, iElement, iAttrs, ngModel) {
ngModel.$formatters.push(function(value){
return 'adasd';
});
}
http://plnkr.co/edit/gZslSeVa2Frq0edEbuWK?p=preview
http://plnkr.co/edit/gZslSeVa2Frq0edEbuWK?p=preview
UPD:
更新:
If you need to update model value directly (not just format value for displaying or parse user input), you can use $setViewValue
(as it was mentioned in Krzysztof Safjanowski answer).
如果您需要直接更新模型值(不仅仅是用于显示或解析用户输入的格式值),您可以使用$setViewValue
(正如 Krzysztof Safjanowski 的回答中提到的那样)。
Other way to modify model value is to use data binding for isolated scope:
修改模型值的其他方法是对隔离范围使用数据绑定:
scope: {
ngModel:'='
},
link: function(scope, iElement, iAttrs, ngModel) {
scope.ngModel= 'adasd';
}
http://plnkr.co/edit/l9kEU03OwI03uVUc6AQ0?p=preview
http://plnkr.co/edit/l9kEU03OwI03uVUc6AQ0?p=preview
If you are not using isolated scope it also posible to update model value, using $parse
service:
如果您不使用隔离范围,也可以使用$parse
服务更新模型值:
link: function(scope, iElement, iAttrs, ngModel) {
$parse(iAttrs.ngModel).assign(scope, 'adasd');
}