Я пытаюсь присвоить свойство объекта услуги с помощью $http
, но у меня запутанные результаты. Почему это не работает (вот мой код):присвоить значение свойства услуги (Angularjs)
.service('config', function ($http) {
var config = {
get_host: function() {
if (online == 0) {
return offlineHost;
}
return onlineHost;
},
online: 'false',
host: 'false',
checkConnection: function() {
//this wont work;
/*
$http.get(this.host + http_url).then(function(response) {
return response.data.ping;
});
*/
//this will work
return 'oke';
},
_load: function() {
this.host = this.get_host();
this.online = this.checkConnection();
this.url_api = this.host + http_url;
if (this.online == 1) {
this.offline_message = 'Maaf aplikasi tidak bisa terkoneksi dengan server atau anda offline';
}
}
};
//run constructor and get value;
config._load();
return config;
}) //end config class
В моем контроллере:
var online = config.online;
alert(online) //return undefined, but the $http request on firebug is showed return value
Большое спасибо, я узнаю об этом :) – navotera