[英]error TS2339: Property 'imageID' does not exist on type 'HTMLElement'
Trying to use the global variable imageID to get the value of getImage, but keep getting error " Property 'imageID' does not exist on type 'HTMLElement'." 尝试使用全局变量imageID来获取getImage的值,但始终获取错误“ 类型'HTMLElement'上不存在属性'imageID'。” .
。 Want to assign the HTMLELement to global variable imageID .
想要将HTMLELement分配给全局变量imageID 。 Is there any way to parse the variable to a string or something?
有什么办法可以将变量解析为字符串? Any suggestions would be appreciated.
任何建议,将不胜感激。
dashboard.component.ts 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
The error occurs because this === button in your code 发生错误是因为代码中的此===按钮
button.addEventListener('click', function(event) {
...
this.imageID = getImage.id;
...
})
if you want to write imageID as a global variable just use window 如果您想将imageID写为全局变量,只需使用window
window.imageID = getImage.id;
if you want to write imageID to class instance attribute use bind 如果要将imageID写入类实例属性,请使用bind
button.addEventListener('click', function(event) {
...
this.imageID = getImage.id;
...
}.bind(this))
or arrow function 或箭头功能
button.addEventListener('click',(event) => {
...
this.imageID = getImage.id;
...
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.