2

コントローラーから $resource $cacheFactory に値を追加し、このデータを複数のコントローラー間で同期させることは可能ですか? 基本的に私が達成しようとしていることは次のとおりです。

1) API から JSON リソースをプルする

2) JSON を $resource ではなく、コントローラー間で使用できる単純な JSON オブジェクトであるかのように操作します。

これを行う「角度のある方法」はありますか、それともローカルストレージを使用して場所リスト全体をキャッシュし、そこから他のすべてを読み書きする必要がありますか?

.factory('places', ['$resource','environment','$cacheFactory',
    function($resource, environment, $cacheFactory) {
    var cache = $cacheFactory('places');
        return $resource(environment.apis.places, {}, {
            query: {
                isArray:true,
                method: 'GET',
                cache: cache
            }
        });
    }
])

.controller('ItemCtrl', function($scope, places) {
    places.query({}, function(result){

        $scope.item = result[0]
    })

    $scope.makeFav = function(index){
        //add a new key to cached places data 
        $scope.item.fav = true
    }
}

.controller('ListCtrl', function($scope, places) {
    places.query({}, function(result){

        $scope.item = result  //should get the updated cache changed by ItemCtrl
    })

    console.log($scope.item[0].fav)  //should = true
}
4

1 に答える 1