调用此$ http请求后(带有server.refresh();
)
MinecraftServer.prototype.refresh = function(){
return $http.get("http://mcping.net/api/" + this.ip).then(this.acceptData);
}
此函数this
是window
对象,而不是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));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句