【问题标题】:Access functions in Angular 7Angular 7 中的访问函数
【发布时间】:2019-03-29 21:08:01
【问题描述】:

在我的组件类(HeaderComponent)中, 每次单击changeBannerArrow()和changeBannerImg()这两个函数时,我都想访问changeBtnColorBg函数

这些函数是在 HTML 端的 onclick 事件上触发的

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 错误 ReferenceError: changeBtnColorBg 不是 定义

有人可以帮忙吗?我是新手

【问题讨论】:

  • 哇,Angular 7 不需要 TypeScript?

标签: javascript angular angular7


【解决方案1】:

尝试进行以下更改:

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
}

}

【讨论】:

    【解决方案2】:
    changeBtnColorBg(this.currentImg, imgSelect);
    

    this.changeBtnColorBg(this.currentImg, imgSelect); // add 'this'
    

    【讨论】:

      【解决方案3】:

      您似乎错过了 changeBannerArrowchangeBannerImg 方法中的 this

        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);
          }
      

      【讨论】:

      • 您好!谢谢你!几秒钟前我发现命令行也显示错误(对此是新的)谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 2015-07-11
      相关资源
      最近更新 更多