简体   繁体   English

错误TS2339:类型“ HTMLElement”上不存在属性“ imageID”

[英]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.

相关问题 错误TS2339:类型“ HTMLElement”上不存在属性“名称” - error TS2339: Property 'name' does not exist on type 'HTMLElement' 角度6:错误TS2339:类型“ HTMLElement”上不存在属性“值” - Angular 6: error TS2339: Property 'value' does not exist on type 'HTMLElement' 如何解决错误“TS2339:&#39;JQuery 类型上不存在属性&#39;仪表&#39;<HTMLElement> &#39;。” - How do I resolve error "TS2339: Property 'gauge' does not exist on type 'JQuery<HTMLElement>'." 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** 错误 TS2339:类型“CustomerTransfert[]”上不存在属性“num” - error TS2339: Property 'num' does not exist on type 'CustomerTransfert[]' 错误TS2339:类型“ {}”上不存在属性“ forEach” - error TS2339: Property 'forEach' does not exist on type '{}' 错误 TS2339:“温度”类型上不存在属性“摄氏度” - error TS2339: Property 'celsius' does not exist on type 'Temperature' 错误 TS2339:“特殊 []”类型上不存在属性“默认” - Error TS2339: Property 'default' does not exist on type 'Special[]' Angular:TS2339:“HTMLElement”/“HTMLTextAreaElement”/等/等类型上不存在“之前”属性 - Angular: TS2339: Property 'before' does not exist on type 'HTMLElement' / 'HTMLTextAreaElement' / etc / etc TS2339:类型“ {}”上不存在属性“ props” - TS2339: Property 'props' does not exist on type '{}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM