0

I want put 10 numbers in my Array , but I get undefined

public arrayDrag: number[];

ngOnInit() {
    this.arrayDrag = new Array(); //or   this.arrayDrag = new Array(10)
}

  rowDragEnd(event) {
    let max = 0;
    this.arrayDrag = new Array();  //or   this.arrayDrag = new Array(10)

    this.gridApi.forEachNodeAfterFilterAndSort(function(node) {
      if (max < 10) {
        this.arrayDrag.push(node.data.point);
        console.log('add', node.data.point);
        max += 1;
      }
    });
  }

I declared to arrayDrag with (0) or (10) and when my event is calls (rowDragEnd), I get error "undefined" the this.arrayDrag.push(

2

1 Answer 1

6

You need to use an arrow function to maintain the scope to your component.

this.gridApi.forEachNodeAfterFilterAndSort((node) => {
  if (max < 10) {
    this.arrayDrag.push(node.data.point);
    console.log('add', node.data.point);
    max += 1;
  }
});

In the code you posted you are using a function so this refers to the function and not your component.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, thanks, But I don't understand why with function () {} undefined ... can you tell me ?
When you declare a function as function () {} within the function this refers to the function and not the component so the function does not have a property of arrayDrag. When you declare the function as () => {} then within the function this refers to the outer scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.