Tran Thien Chien Tran Thien Chien - 5 months ago 15
AngularJS Question

Angularjs - assign variable in callback function

I'm using "this style". And i met a redicilous circumstance

this.showAddressDetail = false;
this.searchPlace = function() {
GoogleService.searchPlace($('#address')[0], function(place) {
this.showAddressDetail = true;
});
};


whenever the callback of GoogleService.searchPlace be triggered. I want to assign true to this.showAddressDetail. But in this case "this" is the callback function.


So how to assign value to this.showAddressDetail?

Answer

You have to assign the "main" this to another variable:

var vm = this;
vm.showAddressDetail = false;
vm.searchPlace = function() {
    GoogleService.searchPlace($('#address')[0], function(place) {
        vm.showAddressDetail = true;
    });
};