Javascript 财产';价值';不存在于类型';元素';
我正在使用Angular创建图像过滤器,但我得到了以下错误Javascript 财产';价值';不存在于类型';元素';,javascript,angular,typescript,Javascript,Angular,Typescript,我正在使用Angular创建图像过滤器,但我得到了以下错误 image = document.querySelector('img'); filterControl = document.querySelectorAll('input[type=range]'); applyfilter(){ let computedFilters =''; this.filterControl.forEach(function(item,index) { computedFilters += item.
image = document.querySelector('img');
filterControl = document.querySelectorAll('input[type=range]');
applyfilter(){
let computedFilters ='';
this.filterControl.forEach(function(item,index) {
computedFilters += item.getAttribute('data-filter') + '(' + item.value + item.getAttribute('data-scale') + ')';
});
this.image.style.filter = computedFilters;
}
错误:
item.value (Property 'value' does not exist on type 'Element'.ts(2339))
这是我的HTML
<input id="sepia" type="range" onchange="applyFilter()" [(ngModel)]="sepia" data-filter="sepia" data-scale="%" step="0.1" min="0" max="1"> Sepia
<span id="Amount_sepia">({{sepia}})</span><br/>
Sepia
({{sepia}})
为什么会出现此错误?
元素
类型是所有html元素(不仅仅是输入)的通用类型,是-该类型中没有值
属性。为了享受使用TypeScript的乐趣,我们需要了解,有时必须将对象强制转换为特定类型。在我们的用例中,类型是HTMLInputElement
,它表示输入(它得到值
属性)。因此:
元素
类型上不存在值
属性。您应该将其转换为inputElement
类型,以便像往常一样访问input
属性let input=item as inputElement
:“您好,我在角度方面有问题”。代码:querySelectorAll
,getElementById
。经典再说一次,这个。是不怎样。有棱角的作品我不知道为什么每个人都在尝试使用Javascript选择器或Angular应用程序中的jQuery来选择DOM中的元素。没有教程使用过它们。但是每个人都尝试过,失败了,然后问为什么querySelectorAll
,getElementById
等在Angular中没有任何业务,因为Angular就是不能以这种方式工作。请按照教程进行操作。非常感谢您在HTMLInputElement中出现错误,我们必须在顶部正确定义。您能告诉我为什么我在inputElement中出现错误吗
this.filterControl.forEach(function(item: HtmlInputElement, index: number) {
if (!item) return; // maybe there are other elements which are not inputs like div, span, etc.
computedFilters += item.getAttribute('data-filter') + '(' + item.value +
item.getAttribute('data-scale') + ')';
});