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=.................

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

4 Answers 4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

this are not working. Just simply i want to show response message on success in views
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;
      })
}]);

1 Comment

this are not working. Just simply i want to show response message on success in views
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

1 Comment

this are not working. Just simply i want to show response message on success in views

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.