简体   繁体   English

无法在Angularjs应用程序的运行功能内注入自定义服务

[英]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. 我是新来Angularjs并写了我的app.js它具有run定义的功能。 I also have a custom service called coreService which I need to inject into the run function. 我还有一个名为coreService的自定义服务,需要将其注入到run函数中。 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 错误:[$ injector:unpr]未知提供程序:$ 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. 我正在使用angularjs-fullstack yeoman生成器来开发应用程序。 Please let me know where I am going wrong. 请让我知道我要去哪里了。 Link to Plnkr - Plnkr link 链接到Plnkr- Plnkr链接

I corrected your code you have many errors there. 我更正了您的代码,您那里有很多错误。 Take a look at PLUNKER You cannot call $scope inside service. 看看PLUNKER您不能在服务内部调用$ scope。

'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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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