简体   繁体   English

“元素”类型上不存在属性“值”

[英]Property 'value' does not exist on type 'Element'

I am creating image filters using Angular but I am getting below error.我正在使用 Angular 创建图像过滤器,但出现以下错误。

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;
}

Error:错误:

item.value (Property 'value' does not exist on type 'Element'.ts(2339))

This is my HTML这是我的 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/>

Why am I getting this error?为什么我会收到此错误?

Element type is general for all html element (not only inputs) and yes - there are no value property in that type. Element类型适用于所有html 元素(不仅是输入),是的 - 该类型中没有value属性。 In order to enjoy using TypeScript, we need to understand that sometimes casting an object to a specific type IS A MUST .为了享受使用 TypeScript 的乐趣,我们需要了解有时将 object 强制转换为特定类型是必须的 in our use-case the type is HTMLInputElement which represens inputs (which got value property).在我们的用例中,类型是HTMLInputElement ,它表示输入(具有value属性)。 So:所以:

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') + ')';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM