Javascript `this` 语句不起作用

Javascript `this` statement not working

调用此 $http 请求后(server.refresh();

MinecraftServer.prototype.refresh = function(){
    return $http.get("http://mcping.net/api/" + this.ip).then(this.acceptData);
}

此函数的 thiswindow 对象,而不是 MinecraftServer 对象:

MinecraftServer.prototype.acceptData = function(data){
    data = data.data

    if(data && data.online){
        this.online = data.online;
        //do more stuff       
    } else { // do more stuff }
}

因此,MinecraftServer 对象不是更新其属性,而是 window 获取属性。

如果这会有所帮助,这是我的工厂代码:

.factory('MinecraftServer',function($http){
    function MinecraftServer(name, ip) { //does stuff }

    MinecraftServer.prototype.acceptData = function(data){
        data = data.data

        if(data && data.online){
            this.online = data.online;
            //do more stuff       
        } else { // do more stuff }
    }
    MinecraftServer.prototype.refresh = function(){return $http.get("http://mcping.net/api/" + this.ip).then(this.acceptData);}
    MinecraftServer.build = function(name, ip){return new MinecraftServer(name, ip)};
    return MinecraftServer;
})

this 作为回调正在使用其他一些 this.

使用.bind:

return $http.get("http://mcping.net/api/" + this.ip).then(this.acceptData.bind(this));