0

I'm just now learning Angular 10 after a long time in AngularJS. I have a working version of this code which does not set the sorted variable, but I'm using this code to learn why I cannot set the variable sorted within my sortById function. The code below produces an error at line 29: ERROR TypeError: Cannot set property 'sorted' of undefined.

Obviously I do not understand the this. concept within the Angular framework.

 import { Component, OnInit } from '@angular/core';
 import { APPLICATIONS } from '../mock-applications';


 @Component({
   selector: 'app-applications',
   templateUrl: './applications.component.html',
   styleUrls: ['./applications.component.scss'],
 })

 export class ApplicationsComponent {

     data = APPLICATIONS;  
     sorted:number = 0;

     constructor() {};

     ngOnInit() {};

     // this function is triggered onClick 
     sortById(){
         // not sorted yet
         if(this.sorted === 0 || this.sorted === 2 ){
             this.data.sort(function (a, b) {
                  this.sorted = 1; // ERRROR HERE
                  console.log('asc');
                  return a.Id - b.Id;
             });
         // sorted 
         } else {
               this.data.sort(function (a, b) {
                    this.sorted = 2;
                    console.log('desc');
                    return b.Id - a.Id;
               });
         }
    
         console.log('sorted ' + this.sorted);
     }

}

1
  • 1
    'this' does not refer to what you think it does. You need to bind it if you want it to reference the ApplicationsComponent instance. Commented Nov 3, 2020 at 18:57

1 Answer 1

3
         // not sorted yet
         if(this.sorted === 0 || this.sorted === 2 ){
             this.data.sort(function (a, b) { <--------- loss of scope
                  this.sorted = 1; // ERRROR HERE
                  console.log('asc');
                  return a.Id - b.Id;
             });
         // sorted 
         }

Declaring the function this way loses the enclosing scope, in this case this probably point to the function instance (tho it could be the window. fun!). If you use an arrow function you will bring in / be bound to the enclosing scope of the component.

 if(this.sorted === 0 || this.sorted === 2 ){
             this.data.sort((a, b) => { <--------- arrow function
                  this.sorted = 1; // ERRROR HERE
                  console.log('asc');
                  return a.Id - b.Id;
             });
         // sorted 
         }

Here is a nice primer for understanding scope better https://medium.com/javascript-in-plain-english/everything-you-wanted-to-know-about-javascript-scope-e991a8288bc

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

1 Comment

Thank you @BrooklynDadCore. I changed both of those to arrow functions and it worked properly. I've done a lot of arrow functions in React, but haven't worked in react for awhile and I'm obviously rusty!

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.