1

using Typescript am I trying to assign the return value from an async service call to a local variable as so;

private searchResult;

public search():void{
             this.DashboardService.dashboardSearch(this.searchParams)
                .then(
                    function (result:ISearchResult) {
                        // promise was fullfilled
                        console.log(result);
                        this.searchResult = result;
                    },
                    function (error) {
                        // handle errors here
                        console.log(error);
                    }
                );
        };
<div id="dashboard-content" ng-controller="dashboardCtrl as vm">
    <h3>{{vm.searchResult}}</h3>
</div>
 

The dashboardSearch service correctly returns an object with data. But i'm unable to assign the data to my local variable.

here is the error + data from Google Chrome console.log

enter image description here

How do I bind the data from my service to my local class variable ?

1
  • 1
    It has to do with the this keyword. There is a previous answer on SO which is very well written on how this works and how to always refer to the correct this instance. stackoverflow.com/a/20279485/1260204 Commented Feb 4, 2016 at 13:21

1 Answer 1

3

private searchResult;

public search():void {
  let that = this;
  
  this.DashboardService.dashboardSearch(this.searchParams)
  .then(function(result:ISearchResult) {
    // promise was fullfilled
    console.log(result);
    that.searchResult = result;
  }, function (error) {
    // handle errors here
    console.log(error);
  });
};

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

Comments

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.