[英]error TS2339: Property 'name' does not exist on type 'HTMLElement'
[英]error TS2339: Property 'imageID' does not exist on type 'HTMLElement'
嘗試使用全局變量imageID來獲取getImage的值,但始終獲取錯誤“ 類型'HTMLElement'上不存在屬性'imageID'。” 。 想要將HTMLELement分配給全局變量imageID 。 有什么辦法可以將變量解析為字符串? 任何建議,將不勝感激。
dashboard.component.ts
import {Component,Input,OnChanges,OnInit,SimpleChanges} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {Emotion} from 'emotion';
import {EmotionService} from 'emotion.service';
@Component({
selector: 'dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
emotions: Emotion[] = [];
emotion: string;
imageID: any;
constructor(private emotionService: EmotionService) {}
ngOnInit() {
this.getEmotions();
// this.makeMatch();
//function to randomize and get face images
var w = document.getElementById('wrapper');
var button = document.getElementById('randomize');
var images = w.children; // inner elements, your image divs
// a function to hide all divs
var hideDivs = function(imgs: HTMLCollection) {
for (var img of < any > imgs) {
(img as HTMLElement).style.display = 'none';
} //for
} //hideDivs
hideDivs(images); // hide all initially
button.addEventListener('click', function(event) {
console.log('');
console.log('%c=============================', "color: blue");
console.log('%c In getFaces method', "color: blue", );
var rnd = Math.floor(Math.random() * images.length); // get random index
hideDivs(images); // hide all images
(images[rnd] as HTMLElement).style.display = 'block'; // show random image
(event.target as HTMLElement).textContent = 'Click one more time!';
var getImage = (images[rnd] as HTMLElement);
//where error occurs
this.imageID = getImage.id;
// this.imageID as HTMLElement = images[rnd].getAttribute('alt');
console.log('%c Image ID for match making: ', imageID );
console.log('%cImage ID: ', "font-weight: bold", getImage.id);
// console.log('%cAll image data:', "font-weight: bold", images[rnd]);
console.log('%c=============================', "color: blue");
console.log('');
}) //button
發生錯誤是因為代碼中的此===按鈕
button.addEventListener('click', function(event) {
...
this.imageID = getImage.id;
...
})
如果您想將imageID寫為全局變量,只需使用window
window.imageID = getImage.id;
如果要將imageID寫入類實例屬性,請使用bind
button.addEventListener('click', function(event) {
...
this.imageID = getImage.id;
...
}.bind(this))
或箭頭功能
button.addEventListener('click',(event) => {
...
this.imageID = getImage.id;
...
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.