Функции доступа в Ангуляр 7 - PullRequest
0 голосов
/ 25 октября 2018

В моем классе Component (HeaderComponent) я хочу получать доступ к функции changeBtnColorBg каждый раз, когда я щелкаю две функции changeBannerArrow () и changeBannerImg ()

Эти функции запускаются при событии onclick на стороне HTML

export class HeaderComponent implements OnInit {
    imgTotal = 3;
    currentImg = 0;

    imgHdr = [];

    changeBannerImg(imgSelect){
        /* some code here */
        changeBtnColorBg(this.currentImg, imgSelect);
    }


    changeBannerArrow(imgSelect){
        from = this.currentImg;

        /* some code here*/
        to = this.currentImg;

        changeBtnColorBg(from, to);

    }

    changeBtnColorBg(from, to){
        this.imgHdr[from].selected = false; //change back to transparent

        this.imgHdr[to].selected = true; //change bg color
    }
}

Но эта структура выдает ошибку

HeaderComponent.html: 15 ERROR ReferenceError: changeBtnColorBg не определено

Может кто-нибудь помочь, пожалуйста?Я новичок в этом

Ответы [ 3 ]

0 голосов
/ 25 октября 2018
changeBtnColorBg(this.currentImg, imgSelect);

до

this.changeBtnColorBg(this.currentImg, imgSelect); // add 'this'
0 голосов
/ 25 октября 2018

Кажется, вы пропустили this в ваших changeBannerArrow и changeBannerImg методах

  changeBannerArrow(imgSelect){
        from = this.currentImg;

        /* some code here*/
        to = this.currentImg;

        this.changeBtnColorBg(from, to);
}


   changeBannerImg(imgSelect){
        /* some code here */
        this.changeBtnColorBg(this.currentImg, imgSelect);
    }
0 голосов
/ 25 октября 2018

попробуйте внести следующие изменения:

export class HeaderComponent implements OnInit {
imgTotal = 3;
currentImg = 0;

imgHdr = [];

changeBannerImg(imgSelect){
    /* some code here */
    this.changeBtnColorBg(this.currentImg, imgSelect); // make change here
}


changeBannerArrow(imgSelect){
    from = this.currentImg;

    /* some code here*/
    to = this.currentImg;

    this.changeBtnColorBg(from, to); // make change here

}

changeBtnColorBg(from, to){
    this.imgHdr[from].selected = false; //change back to transparent

    this.imgHdr[to].selected = true; //change bg color
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...