2016-04-07 5 views
0

ファクトリ/サービスを使用してコントローラ間でデータを共有することについて多くのことがありますが、私の問題に該当するものは見つかりません。私は答えを間違って解釈しているか、これは私が聞いている正当な質問です!うまくいけば、後者。工場と約束事を使用しているコントローラ間で共有されたデータ

コントローラ2は、新しいhttp呼び出しが行われ、ファクトリイメージオブジェクトが更新されたときを認識したいと思います。現時点では一度解決され、その後の更新は無視されます。何が見落とされていますか?

マイビュー:

<div> 
    <ul class="dynamic-grid" angular-grid="pics" grid-width="150" gutter-size="0" angular-grid-id="gallery" refresh-on-img-load="false" > 
     <li data-ng-repeat="pic in pics" class="grid" data-ng-clock> 
      <img src="{{pic.image.low_res.url}}" class="grid-img" data-actual-width = "{{pic.image.low_res.width}}" data-actual-height="{{pic.image.low_res.height}}" /> 
     </li> 
    </ul> 
</div> 

工場:

.factory('imageService',['$q','$http',function($q,$http){ 

    var images = {} 
    var imageServices = {}; 
    imageServices.homeImages = function(){ 
    console.log('fire home images') 
     images = $http({ 
     method: 'GET', 
     url: '/api/insta/geo', 
     params: homeLoc 
     }) 
    }; 
    imageServices.locImages = function(placename){ 
    console.log('fire locImages') 
     images = $http({ 
     method: 'GET', 
     url: '/api/geo/loc', 
     params: placename 
     }) 
    }; 
    imageServices.getImages = function(){ 
    console.log('fire get images', images) 
    return images; 
    } 
    return imageServices; 
}]); 

controller1:

angular.module('trailApp.intro', []) 

.controller('introCtrl', function($scope, $location, $state, showTrails, imageService) { 
    // run the images service so the background can load 
    imageService.homeImages(); 

    var intro = this; 

    intro.showlist = false; 
    intro.data = []; 

    //to get all the trails based on user's selected city and state (collected in the location object that's passed in) 
    intro.getList = function(location) { 

     intro.city = capitalize(location.city); 
     intro.state = capitalize(location.state); 
     //get placename for bg 

     var placename = {placename: intro.city + ',' + intro.state}; 
     imageService.locImages(placename); 
... do other stuff... 

コントローラ2:

angular.module('trailApp.bkgd', []) 
.controller('bkgdCtrl', ['$scope','imageService', 'angularGridInstance', function ($scope,imageService, angularGridInstance) { 
    $scope.pics = {}; 
    imageService.getImages().then(function(data){ 
     $scope.pics = data; 
     console.log($scope.pics); 
    }); 
}]); 

答えて

0

あなたのコントローラ2の実装は一度だけの画像を持って、あなたはおそらく、更新に保つために$watchが必要:

angular.module('trailApp.bkgd', []) 
.controller('bkgdCtrl', ['$scope','imageService', 'angularGridInstance', function ($scope,imageService, angularGridInstance) { 
    $scope.pics = {}; 

    $scope.$watch(function(){ 
    return imageService.getImages(); // This returns a promise 
    }, function(images, oldImages){ 
    if(images !== oldImages){ // According to your implementation, your images promise changes reference 
     images.then(function(data){ 
     $scope.pics = data; 
     console.log($scope.pics); 
     }); 
    } 
    }); 

}]); 
+0

おかげで、このソリューションは動作します。私は$ watchが避けるべきものだと思ったので、私は矛盾するメッセージを得ていたと思います。乾杯! – abigwonderful

関連する問題