Murugan
Murugan

Reputation: 87

Angular - How to set the value inside the function?

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

Upvotes: 0

Views: 1045

Answers (1)

Lucas Palomo
Lucas Palomo

Reputation: 156

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

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

See the example bellow:

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

In your code, replace the other functions with arrow functions

Observations:

  • Arrow functions are allowed in es6 or more than

Reference:

Upvotes: 4

Related Questions