为什么我得到AngularJS错误:$ injector:modulerr模块错误

时间:2017-07-22 01:16:22

标签: angularjs

我正在尝试启动并运行一个小角度应用程序,但每次加载应用程序时都会收到相同的错误:

Error: $injector:modulerr
Module Error

Failed to instantiate module app due to:
Error: [$injector:unpr] 
http://errors.angularjs.org/1.6.5/$injector/unpr?p0=%24scope
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:7:76
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:46:65
at d (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:43:280)
at e (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:44:6)
at Object.invoke (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:44:91)
at d (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:42:237)
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:42:376
at p (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:8:7)
at g (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:42:138)
at gb (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js:46:251

以下是我的index.html文件和我的app.js文件的片段:

var app = angular.module('app', []);

app.service('languageService', function($resource){

});


app.config(function($scope){
    $scope.submit = function() {
      console.log("yay");
    }
});
<html lang="en" ng-app="app" class="no-js"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>WIT</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="images/computer.ico"/>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

  <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="app.css">
</head>
<body>

  <div class="container">
    <div class="row">
      <h1><span class="glyphicon glyphicon-pencil"></span>RecommendHer</h1>
    </div>
  </br>
  </br>
    <div class="row">
      <div class="col-xs-12">
        <p>Please paste job description below:</p>
      </div>
    </div>
    <div class="row">
      <div class="col-xs-12">
        <textarea ng-model="jobDescription"></textarea>
      </div>
    </div>
  </br>
    <div class="row">
      <div class="col-xs-12">
        <button class="btn btn-default" ng-click="submit()">Submit</button>
      </div>
    </div>
  </div>

  <div ng-view></div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
  <script src="app.js"></script>
  <script src="services/languageService.js"></script>
</body>
</html>

如果有任何其他信息有用,请告诉我!谢谢。

1 个答案:

答案 0 :(得分:2)

您不能将$ scope注入配置。 $ scope只能在controller和directive中访问。 我想你在寻找控制器?

app.controller('yourCtrl', function($scope){ // your code });

var app = angular.module('app', []);

app.service('languageService', function($resource){

});


app.config(function(){
    
});
<html lang="en" ng-app="app" class="no-js"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>WIT</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="images/computer.ico"/>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

  <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="app.css">
</head>
<body>

  <div class="container">
    <div class="row">
      <h1><span class="glyphicon glyphicon-pencil"></span>RecommendHer</h1>
    </div>
  </br>
  </br>
    <div class="row">
      <div class="col-xs-12">
        <p>Please paste job description below:</p>
      </div>
    </div>
    <div class="row">
      <div class="col-xs-12">
        <textarea ng-model="jobDescription"></textarea>
      </div>
    </div>
  </br>
    <div class="row">
      <div class="col-xs-12">
        <button class="btn btn-default" ng-click="submit()">Submit</button>
      </div>
    </div>
  </div>

  <div ng-view></div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
  <script src="app.js"></script>
  <script src="services/languageService.js"></script>
</body>
</html>