角度-如何在函数内设置值? [英] Angular - How to set the value inside the function?

查看:53
本文介绍了角度-如何在函数内设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法设置 this.isHiddenColumnAvail 值.如何设置该值?

I am not able to set the this.isHiddenColumnAvail value. How to set this value?

checkHiddenRowAndColumns(file) {
    const workbook = new Excel.Workbook();
    const arryBuffer = new Response(file).arrayBuffer();
    arryBuffer.then(function (data) {
      workbook.xlsx.load(data)
        .then(function () {
          const worksheet = workbook.getWorksheet(1);

          //check hidden columns
          for(var i=0; i < worksheet.columns.length; i++) {
            if(worksheet.columns[i].hidden == true){
              this.isHiddenColumnAvail = true;
              break;
            }
          }

          //check hidden rows
          worksheet.eachRow(function (row,rowNumber) {
            if(row.hidden == true) {
              this.isHiddenColumnAvail = true;
            }
          });
        });
    });
  }

推荐答案

只需将函数更改为箭头函数即可.

It's simple you need to change the function to an arrow function.

使用箭头功能,您可以使用 this ,从而允许您访问外部/全局变量.

Using Arrow functions you can use this, allowing you access outside/global variables.

请参见下面的示例:

      worksheet.eachRow((row,rowNumber) => {
        if(row.hidden == true) {
          this.isHiddenColumnAvail = true;
        }
      });

在您的代码中,将其他功能替换为箭头功能

In your code, replace the other functions with arrow functions

观察:

  • 在es6或更高版本中允许使用箭头功能

参考:

这篇关于角度-如何在函数内设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆