두 가지 문제가 있습니다. $ http 응답에서 값을 가져 와서 몇 개의 DOM 개체를 업데이트해야하는 변수를 채우려고합니다. 문제는 $ http 서비스를 호출 한 함수가 완료되고 변수가 업데이트되지만 어디서나 업데이트되지 않는 타이밍 문제가있는 것 같습니다. 변수에 시계를 넣으려고했는데 페이지가 처음로드 될 때만 실행되는 것처럼 보입니다. 나는 하루 종일이 모든 것을 읽고 있었고 작동하지 않는 답을 찾는 것 같습니다.
app.controller('MainCtrl', ['$scope', '$http', 'waciServ', function($scope, $http, waciServ) {
"use strict";
$scope.currentSource = waciServ.activeSource;
$scope.$watch('waciServ.activeSource', function(newValue, oldValue) {
$scope.currentSource = newValue;
console.log('Watcher! ' + newValue);
}/*, true*/);
$scope.getActiveSource = function() {
$scope.currentSource = waciServ.getStringByName("active_device");
};
}]);
app.service('waciServ', function($http) {
var self = this;
this.waciIP = location.host;
this.activeSource = '';
this.getStringByName = function (name) {
$http.post("http://" + self.waciIP + "/rpc/", "method=GetVariableByName¶m1=" + name + "&encoding=2")
.then (function (response) {
var was_error = self.read(response.data);
if (was_error == '1') { //active_device is not set
self.assignVariable(name, "none");
self.activeSource = "none";
return self.activeSource;
} else {
var varId = parseInt(self.read(response.data));
$http.post("http://" + self.waciIP + "/rpc/", "method=GetVariableValue¶m1=" + varId + "&encoding=2")
.then (function (response) {
self.activeSource = self.read(response.data);
return self.activeSource;
});
}
}, function (error) {
console.log("error: " + error.data);
});
};
});
내가 반환 화재 전에 CONSOLE.LOG 권리를 배치하고 내가 원하는 것을 가지고 있지만, 다른 CONSOLE.LOG 컨트롤러 쇼 '정의되지 않은'내 기능에 배치 것을 볼 수 있습니다.
무엇을 제공합니까? 미리 감사드립니다.