0

got error of scope not defined when i use for response message:

$scope.responsemessage = response;

then I added $scope here like this:

app.service('fileUpload', ['$http', '$scope', function ($http, $scope) {

When I added above $scope got this error Error: [$injector:unpr] http://errors.angularjs.org/1.4.8/$injector/unpr?p0=.................

  • 2
    possible duplicate of http://stackoverflow.com/questions/22898927/injecting-scope-into-an-angular-service-function $scope” is an object instance of a controller. “$scope” object instance get’s created when “ng-controller” directive is encountered. – Banik Feb 19 '16 at 11:20
  • may be try $rootScope if you want, that will visible in service too – subash Feb 19 '16 at 11:26
  • this are not working. Just simply i want to show response message on success in views – Swamy Reddy Feb 19 '16 at 11:53

4 Answers4

1

Service don't have scope.

If you wanna to use scope inside service , have to pass it from controller .

Like this

In controller

fileUpload.checkFile($scope.responsemessage);

In service

function checkFile(respMsg)
{
   console.log(respMsg);
}
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
1

$scopes are not available from services, it should only be used inside controllers.

Louie Almeda
  • 5,366
  • 30
  • 38
1

In angular $scope is available in controllers and services are used as a dependency for data provider.

You just can't use $scope in the service and from service you can return a promise object like:

app.service('fileUpload', ['$http', function($http) {
    return $http.post('url')
}]);

Now you can inject this service to update a variable on the controller's $scope:

app.controller('cntrl', ['$scope', 'fileUpload',  function($scope, fileUpload){
      $scope.responsemessage = fileUpload.then(function(data){
          return data;
      })
}]);
Jai
  • 74,255
  • 12
  • 74
  • 103
0

You can not use $scope in Services as well as in Factories. You can call service by giving $scope value as a parameter and save the return value to the $scope variable