How to upgrade a certain function from AngularJS to Angular 6 or higher?

Issue

I am currently upgrading our system from AngularJS to Angular 6.

And I cross upon a certain function that uses .then like this:

searchService.searchSubject(subjectId)
 .then(function(subject) {
        if(subject && subject.length > 0){
            //code block;
        } else {
            //code block;
        }
    })
.catch(function(error) {
        //code block;
    });
}

How do I change this function to Angular 6?

Solution

Really you should use flat arrow sintax

//include "this" -remember that you sould inject the service in constructor-
this.searchService.searchSubject(subjectId)
 .then((subject)=> {
        if(subject && subject.length > 0){
            //code block;
        } else {
            //code block;
        }
    },
     (error)=>{
        //code block
     }
    )

Answered By – Eliseo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published