簡體   English   中英

“元素”類型上不存在屬性“值”

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

我正在使用 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;
}

錯誤:

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/>

為什么我會收到此錯誤?

Element類型適用於所有html 元素(不僅是輸入),是的 - 該類型中沒有value屬性。 為了享受使用 TypeScript 的樂趣,我們需要了解有時將 object 強制轉換為特定類型是必須的 在我們的用例中,類型是HTMLInputElement ,它表示輸入(具有value屬性)。 所以:

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