How to avoid data mix between $scope variables in ng-repeat when it is broadcasted in other controller?

71 Views Asked by At

I have two controllers. In one controller I am storing the data in scope variable for different categories and for different weeks and days. Here is the function for the same:

$scope.fetchWeekList = function(type) {
    $scope.loading = true;
    var paramtype = (type == 'mo')?'Mobiles':((type == 'ta')?'Tablets':((type == 'la')?'Laptops':'TVs'));
    var weekListUrl = url + "/" + paramtype;
    var request = $http({
        method: "GET",
        url: weekListUrl, 
        headers: { 'Accept' :'application/json','Content-Type' :'application/json', 'Accept-Language': 'en'}
     });

     request.success(
       function(data) {
           $scope.weekList = data.object;
           $scope.loading = false;
     });

     request.error(
        function(data, status) {
            console.log(status);
            $scope.weekList = data || "Request failed";
            $scope.loading = false;
     });
};

Please pat attention that I am fetching the data for the week lists for all the categories with this single function.

Then I am using this:

$scope.$on('fetchSaleDetails', function(event,type) {
    $scope.fetchWeekList(type);
    }

Then I am broadcasting it in the other controller like this:

$rootScope.$broadcast('fecthSaleDetails','mo');
$rootScope.$broadcast('fecthSaleDetails','ta');
$rootScope.$broadcast('fecthSaleDetails','la');

But when I switch the company the weeks of one category appears in the other and when I click again on the company the data changes. This is the function to update company.

$scope.updateCom = function(corresCom) {
    $("html, body").animate({scrollTop: 0 }, "slow");

    $rootScope.$broadcast('updateComDetail',corresCom);
    $rootScope.$emit('fetchSaleDetails','mo');
    $rootScope.$broadcast('fecthSaleDetails','mo');
    $rootScope.$broadcast('fecthSaleDetails','ta');
    $rootScope.$broadcast('fecthSaleDetails','la');

    $scope.selectedCom = corresCom;

};

I would be grateful if someone can tell me the issue here. I have tried my best but no luck.

Thanks.

0

There are 0 best solutions below