vendredi 11 septembre 2015

Angularjs Service does not work

I define a Service to share a variable between two controllers, but when i set the variable in a controller and then get this from another controller it does not get the correct value , this is the service:

 App.service("ProductService", function () {
    var productTotalCount = {};
    return {
        getproductTotalCount: function () {
            return productTotalCount;
        },

        setproductTotalCount: function (value) {
            productTotalCount = value;
        }
    }
});

and this is the controller which i set productTotalCount:

 App.controller("ProductController", function ($scope, $http, $rootScope, ProductService) {
    $scope.GetAllProducts = $http.get("GetAllProductsInformation").success(function (data) {

        $rootScope.Products = data.Data;
        ProductService.setproductTotalCount(data.TotalCount); // i set productTotalCount here and it's value became 19
    });
    $scope.editProduct = function (data) {

        $scope.model = data;
        $rootScope.$broadcast('modalFire', data)
    }
});

and when i get the productTotalCount in this controller it return object instead of 19 :

 App.controller('Pagination', function ($scope, ProductService) {
    debugger;
    $scope.totalItems = ProductService.getproductTotalCount(); // it should return 19 but return object!!
    $scope.currentPage = 1;
    $scope.itemPerPage = 8;
});

what is the problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire