'this' is undefined inside the foreach loop

后端 未结 3 1808
太阳男子
太阳男子 2020-12-28 12:19

I am writing some typescript code and iterating an array. Inside the loop, I am trying to access \'this\' object to do some processing as:

console.log(\'befo         


        
3条回答
  •  执笔经年
    2020-12-28 12:47

    Add the this as a parameter for callback.

    Adding }, this); instead of }.bind(this)); should resolved issue in Angular.

    Thus, should look like:

    myarray.days.forEach(function(obj, index) {
        console.log('before transform, this : ' + this);
        this.datePipe.transform...
    }, this);
    

提交回复
热议问题