0

I am new to Angularjs and have written my app.js which has the run function defined. I also have a custom service called coreService which I need to inject into the run function. When I inject, I get an error stating

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- coreService http://errors.angularjs.org/1.4.7/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20coreService

I am using the angularjs-fullstack yeoman generator to develop the application. Please let me know where I am going wrong. Link to Plnkr - Plnkr link

zilcuanu
  • 3,451
  • 8
  • 52
  • 105

2 Answers2

1

I corrected your code you have many errors there.Take a look at PLUNKER You cannot call $scope inside service.

'use strict';

angular.module('myApp')
  .service('coreService', function () {
      var sayHi=function(){
        console.log("Hi..");
      }

      return {
        sayHi:sayHi
      }
  });





<!DOCTYPE html>

    <html>

      <head>
        <script data-require="angular.js@1.4.2" data-semver="1.4.2" src="https://code.angularjs.org/1.4.2/angular.js"></script>
        <script src="https://code.angularjs.org/1.4.6/angular-route.js"></script>
        <link rel="stylesheet" href="style.css" />


      </head>

      <body ng-app="myApp">
        <h1>Hello Plunker!</h1>

        <script src="script.js"></script> 
        <script src="coreService.js"></script>
      </body>

    </html>

And rename your coreService to coreService.js

Elvin Valiev
  • 454
  • 6
  • 15
  • Thanks for the answer. I tried injecting the rootScope inside the service and it started working. Does that mean that if we want to update some variable which will be used in the application, we need to inject the rootScope and set the variables in the rootScope inside Service? – zilcuanu Nov 26 '15 at 06:34
  • Yes, $rootScope is the global scope ,so you can inject it to services, but you cannot do the same for $scope . However if you want to interact with $scope you can pass it's variables to the service as an argument of functions and modify it and return modified data. – Elvin Valiev Nov 26 '15 at 06:36
0

Rename coreService to coreService.js and include coreService.js after script.js.

Tarun Dugar
  • 8,921
  • 8
  • 42
  • 79
  • Did the changes but still the same error. However, I was able to resolve the issue by removing the `$scope` as dependency in the `coreService` service. updated Link - http://plnkr.co/edit/2zpnQ1tDbpzmHukL0739?p=preview – zilcuanu Nov 26 '15 at 06:27
  • Yes forgot to mention that. $scope is never injected in a service. – Tarun Dugar Nov 26 '15 at 06:27