AngularJS에 새로운 문제가 있습니다. 2 개의 컨트롤러에서 "공유"변수를 읽고, 업데이트 할 수 있어야하기 때문에, 두 컨트롤러 모두에 팩토리가 삽입되어 있다고 생각했습니다. 데이터는 http 요청을 통해로드되지만 요청이 완료되면 var는 업데이트되지 않습니다. 여기에 내 코드입니다 :AngularJS - 컨트롤러에서 공장 변수가 업데이트되지 않습니다.
//Common factory
app.factory('CurrentGallery',['$q','$http',function($q,$http){
var data = null;
//HTTP request to API
var loadImages = function(query){
$http.post("https://mywebsite.com/api/?"+query).then(function(result){
update(result.data);
});
}
//Update the data var
var update = function(data){
data = data;
console.log("Update called", data);
}
return {
loadImages: loadImages,
update: update,
data: data
}
}]);
//The controller
app.controller("PhotoBestController", ['$scope', 'CurrentGallery', function ($scope,CurrentGallery) {
//$scope.pics is basically the variable I want to update inside my controller
$scope.pics = CurrentGallery.data;
//Send the data of the query for the API
CurrentGallery.loadImages("userInfo=true&exifInfo=true&order=tot_like,DESC");
$scope.$watch(CurrentGallery, function(){
console.log("CurrentGallery has changed");
});
}]);
입니다 내 콘솔에서 얻을 로그 :
- CurrentGallery가 변경
- 라는 업데이트 개체 {...}는
그래서 그것을 CurrentGallery가 처음으로 업데이트 된 것처럼 보이지만, null 일 때 공장에서 업데이트 되더라도 $ scope.pics 변수는 업데이트되지 않습니다.
제안 사항?
이 당신을 도울 수 http://stackoverflow.com/questions/30596258/watch-factory-variable- with-angular –
@VinodLouis 그것은 문제를 해결하는 것 같지 않습니다! 그것은 정확히 동일한 결과를 제공하므로 아무 것도 업데이트하지 않습니다. – Wendigo