Angularjs - 在回调函数中赋值变量

Tra*_*ien 2 javascript angularjs

我正在使用controller as-syntax并遇到了一个荒谬的情况

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

每当触发GoogleService.searchPlace的回调时.我想为this.showAddressDetail 指定true.但在这种情况下this是回调函数.
那么如何为this.showAddressDetail赋值?

mic*_*lem 5

您必须将"main"分配this给另一个变量:

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