Skip to content Skip to sidebar Skip to footer

How To Use $scope In Angularjs Service

I am storing my api response in a $scope variable. I have written in my main controller. Instead of that i am planning to write in one service file and need to use it in all contro

Solution 1:

Can be used but not a good case

MainController.js :

commonService.something($scope);

CommonService.js:

app.service('commonService', ['$timeout', '$rootScope', 'apiService',
    function ($timeout, $rootScope, apiService) {
        var something = function (scope) {
            apiService.getResponseData(scope.UserId).then(function (reply) {
                scope.responseData = reply.data;
                $timeout(function () {
                    reply.data.forEach(function (card) {
                        if (card.Category == 'Apple') {
                            console.log("Apple");
                        } else if (card.Category == 'Google') {
                            console.log("Google");
                        } else if (card.Category == 'Microsoft') {
                            console.log("Microsoft");
                        } else if (card.Category == 'Amazon') {
                            console.log("Amazon");
                        }

                    });
                });
            });
        };

        return {
            'something': something
        }
    }
]);

Post a Comment for "How To Use $scope In Angularjs Service"