break cache view true ionic

1

I load the products in this controller ...

.controller('productosCtrl', function($scope, $http, $q, $location, $ionicLoading, Productos) {
    Productos.productos().then(function(argument, $timeout) {
        console.log(argument)
        $scope.tiendaD = {
            nameS: argument.store.name,
            addressS: argument.store.address,
            idS: argument.store.id,
            logoS: argument.store.logo.logo.url
        };
        $scope.producto = []
        for (var i = 0; i <= argument.store.products.length - 1; i++) {
            $scope.producto.push({
                id: argument.store.products[i].id,
                name: argument.store.products[i].name,
                description: argument.store.products[i].description,
                category: argument.store.products[i].category,
                store: argument.store.products[i].store,
                price: argument.store.products[i].price,
                picture: argument.store.products[i].picture.picture.url
            });

        }
        $scope.$broadcast('scroll.refreshComplete');
    })
})

My factory

.factory('Productos', function($q, $http, $location) {
    var Api = {};
    $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    Api.productos = function() {
        var defer = $q.defer();
        var paramId = $location.search().id;
        var urlaux = "http://abbie-core.herokuapp.com/api/stores/" + paramId + "?token=" + localStorage.getItem('token') + "&user_id=" + localStorage.getItem('user_id');
        $http({
            method: 'GET',
            url: urlaux,
        }).success(function(data) {
            defer.resolve(data);
        })
        return defer.promise;
    }
    return Api;
})

... and I print them in the view with a ng repeat , where I print these products I put cache view true , but if I add a new product and leave, and enter the view, I can not see the other product I have to reload the view.

What can I do to break that cache when there is a new product?

    
asked by ingFR 20.10.2016 в 04:21
source

1 answer

0

The error is that you never keep them where you must guarlos .. Tu when you save a new product you save it in $scope.producto but when you re-enter your controller you go through Productos.productos() , I imagine you have some factory that obtains the products and also keeps them called Products.

I will leave a code, I did not put 100% in your context but I think that here lies the error

.factory('Productos', function() {
    productos = [];
    return {
         productos: function(){
            var defer = $q.defer();
            var paramId = $location.search().id;
            var urlaux = "http://abbie-core.herokuapp.com/api/stores/" + paramId + "?token=" + localStorage.getItem('token') + "&user_id=" + localStorage.getItem('user_id');
            $http({
                method: 'GET',
                url: urlaux,
            }).success(function(data) {
                defer.resolve(data);
            })
            return defer.promise;
         }, 
         add : function(producto){
             //Llamar a funcion que agrega en tu API los productos
         }
         ....
    }
 )}

To add it from

for (var i = 0; i <= argument.store.products.length - 1; i++) {
        var produ = {
            id: argument.store.products[i].id,
            name: argument.store.products[i].name,
            description: argument.store.products[i].description,
            category: argument.store.products[i].category,
            store: argument.store.products[i].store,
            price: argument.store.products[i].price,
            picture: argument.store.products[i].picture.picture.url
        }
        Producto.add(produ);
 }
 $scope.producto = Producto.productos();

EDIT

Now that you publish your factory , I realize that whenever you want to see your products you call your API and return them, IN THIS CASE what you have to do is create in your API a function that Receive a new product and add it. That will make it appear every time you add a new one since your products are always from your API .

    
answered by 20.10.2016 в 14:18