0

I've ProductService and ProductsController. ProductService have ProductService.Products = []; variable which contains all the Products information. I access this Products-information in ProductsController and stores in variable named $scope.Products = [];.

Problem is some other service also using "ProductService", and updating "Products Info", using "UpdateInfo" function exposed in ProductService. Now these changes are not getting reflected in ProductsController variable $scope.Products = [];.

This is my code.

sampleApp.factory('ProductService', ['$http', '$q', function ($http, $q){
    var req = {
        method: 'POST',
        url: 'ProductData.txt',
        //url: 'http://localhost/cgi-bin/superCategory.pl',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }//,
        //data: { action: 'GET' }
    };  
    var ProductService =  {};
    ProductService.Products = [];
    return {
        GetProducts: function () {
            var defer = $q.defer();
            $http(req).then(function(response) {
                ProductService.Products = response.data;
                defer.resolve(ProductService.Products);
            }, function(error) {
                defer.reject("Some error");
            });
            return defer.promise;
        },

        UpdateInfo: function (ProductID, VariantID) {

            for (i in ProductService.Products) {
                if (ProductService.Products[i].ProductID == ProductID) {
                    for (j in ProductService.Products[i].Variants)  {
                        if (ProductService.Products[i].Variants[j].VariantID == VariantID) {
                            ProductService.Products[i].Variants[j].InCart = 1;    /* Updating Info Here, But its not reflecting */ 
                            break;
                        }
                    }
                    break;
                }
            }
        }
    };
}]);


sampleApp.controller('ProductsController', function ($scope, $routeParams, ProductService, ShoppingCartService) {

    $scope.Products = [];

    $scope.GetProducts = function() {
        ProductService.GetProducts().then
        (
            function(response) {
                $scope.Products = response;
            },
            function(error) {
                alert ('error worng');
            }
        );
    };

    $scope.GetProducts();



});

Can some one help me how to solve this issue?

2 Answers 2

2

You can create a $watch on ProductService.Products in your controller. When the value changes, you can update $scope.Products with the new value.

$scope.$watch('ProductService.Products', function() {
    $scope.Products = ProductService.Products;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. One more thing, every time my code route to "ProductsController", its makes a call to $scope.GetProducts(); which internally calls ProductService and then http service, so everytime its making a http request. which updates modified ProductInfo to new response. How to avoid this?
Services are injected, thus a shared resource. Think of it like having two variables pointing to a reference type in C#, VB or the like. If you change it one place, it's changed everywhere.
-1

Try assigning ProductsService.Products to $scope.products.

    $scope.GetProducts = function() {
            ProductService.GetProducts().then
            (
                function(response) {
                    $scope.Products = ProductService.Products;
                },
                function(error) {
                    alert ('error worng');
                }
            );
        };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.