3

studentService.js

   app.factory('saveStudentService',['$http','$scope',function ($http,$scope) { 
            var studentData = {};
            studentData.save = function(jsondata){
                var action = "student";
                var method = "POST";
                $http({
                    url: action,
                    method: method,
                    headers: {'Content-Type': 'application/json'},
                    data: jsondata
                }).success(function(data, status, headers, config) {
                    toastr.success(status +' : Data has been submitted successfully ');
                }).error(function(data, status, headers, config) {
                    toastr.error(status + ' : Data has not been submitted successfully ');
                });
            };
            return studentData;
        }]);

I am getting this error

angular.js:13642Error: [$injector:unpr] http://errors.angularjs.org/1.5.6/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20saveStudentService
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js:6:412

If from studentService.js, $scope is being removed, i.e

app.factory('saveStudentService',['$http',function ($http) { 

this code is working properly, and not getting any error message in console.

Following is the studentController.js file from where this studentService is being called.

StudentController.js

app.controller('saveStudentCtrl',['$scope', 'saveStudentService', function($scope,saveStudentService) { 
    $scope.submit_create_student = function() {
        var jsondata = $scope.student;
        saveStudentService.save(jsondata);
    }

}]);

but if same thing i.e $scope is being added in the updateStudentService then this code is working as expected.

app.controller('updateStudentCtrl',['$scope','retriveStudentService', 'updateStudentService', function($scope,retriveStudentService, updateStudentService) {    
    $scope.getStudentDataFromServer = function() {      
        retriveStudentService.get();
    };
    $scope.submit_update_student = function(e) {
        updateStudentService.update();
    }

}]);

Could someone please clarify, what is happening here. Though able to use same thing in one place, but not able to use same process at someother place.

4
  • Possible duplicate of Accessing $scope in AngularJS factory? Commented Aug 28, 2016 at 17:26
  • you mean to say in studentController.js and studentService.js ? Commented Aug 28, 2016 at 17:29
  • But why do you need $scope in factory? IMO there is something wrong in design if you need scope in your factory/service Commented Aug 28, 2016 at 17:32
  • thanks for your suggestion :) Commented Aug 28, 2016 at 19:12

1 Answer 1

5

You cannot inject scope into services. You can inject it to controllers.

Sign up to request clarification or add additional context in comments.

4 Comments

hey thanks for your comment. I have just started working with angularjs.
from my code if you find something else that need to be rectified, please let me know.. thanks
Your code looks good, try using $resource instead of $http when possible.
Yes, you don't inject $scope into services. Sometimes you would inject $rootScope into a service if for example it needs to broadcast system-wide events. Services are designed to be global objects, while $scope is designed to hold contextual data.

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.