角度-在每个输入上使用$ sanitize [英] Angular - Use $sanitize on every input
问题描述
在服务器端,我会清理进来的所有东西.
On my server side I sanitise everything coming in.
在Angular应用客户端上,我在某些情况下(例如联系表格,但是现在我想我不应该将其应用于每个输入字段吗?
On my Angular app client side I've done the same in some instances e.g. contact forms however am now thinking shouldn't I just apply it to every input field?
所以我看过 $ sanitize('some string')
,但是有一种方法可以仅在某些顶级应用程序级别上应用它,而不必为每个实例输入字段都输入它吗?
So I've seen $sanitize('some string')
however is there a way to just apply it at some top app level rather than having to enter that for every instance input fields is?
(假设这样做是明智之举-如果不愿意听取建议).
(Assuming this is a wise thing to do - if not keen to hear suggestions).
谢谢.
推荐答案
您可以创建一个称为输入"的指令,然后将其应用于您应用程序的所有输入.
You can create a directive that you will call "input", then it will apply to all inputs of your app.
在指令中,您可以将解析器添加到$ ngModel中,以在值更改时自动应用$ sanitize.
Inside the directive you can add a parser to $ngModel to apply automatically $sanitize when value changes.
它看起来像这样:
myApp.directive('input', function($sanitize) {
return {
restrict: 'E',
require: '?ngModel',
link: function (scope, element, attrs, ngModel) {
if(ngModel !== undefined){
ngModel.$parsers.push(function(value){
return $sanitize(value);
});
}
}
};
});
这篇关于角度-在每个输入上使用$ sanitize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!