1

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 변수는 업데이트되지 않습니다.

제안 사항?

+0

이 당신을 도울 수 http://stackoverflow.com/questions/30596258/watch-factory-variable- with-angular –

+1

@VinodLouis 그것은 문제를 해결하는 것 같지 않습니다! 그것은 정확히 동일한 결과를 제공하므로 아무 것도 업데이트하지 않습니다. – Wendigo

답변

0

UPDATE
내가이 글의 코드 논리를 다음 : AngularJS : How to watch service variables?

app.factory('CurrentGallery',['$q','$http',function($q,$http) { 
    var updateCallbacks = []; 
    var data = null; 

    var loadImages = function(query) { 
    $http.post("https://mywebsite.com/api/?"+query).then(function(result) { 
     angular.forEach(updateCallbacks, function(callback) { 
     callback(result.data); 
     }); 
    }); 
    } 

    var registerUpdateCallback(callback) { 
    updateCallbacks.push(callback); 
    } 

    return { 
    loadImages: loadImages, 
    registerUpdateCallback: registerUpdateCallback 
    } 
}]); 

app.controller("PhotoBestController", ['$scope', 'CurrentGallery', function($scope,CurrentGallery) {  
    CurrentGallery.registerUpdateCallback(function(data) { 
    console.log("updated") 
    }); 
    CurrentGallery.loadImages("userInfo=true&exifInfo=true&order=tot_like,DESC"); 
}]); 
+0

예, 그것에 대해 생각했지만 약속을 사용하는 경우 잠재적 인 2nd 컨트롤러에서 CurrentGallery.data var을 공유하려는 사람의 변경을 어떻게 감지 할 수 있습니까? – Wendigo

0
I think your data is updated in factory only. So for updating it in controller you have to get it again from the factory. 

So where you put watch in your controller re-assign the scope variable 

$scope.$watch(CurrentGallery, function(){ 
     $scope.pics = CurrentGallery.data; 
     console.log("CurrentGallery has changed"); 
}); 
+0

그게 내가 한 일이지만, "CurrentGallery가 변경되었습니다."라는 로그를 발사하지도 않았습니다. – Wendigo

+0

그것은 내가 여기에 보관하도록 게시 한 코드에 반드시 있어야하는 것은 아닙니다. 그래서 그것은 효과가있다. –