简体   繁体   中英

unable to inject custom service inside the run function of Angularjs application

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

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

将coreService重命名为coreService.js,并在script.js之后添加coreService.js。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM