无法注入服务

Unable to inject service

我没有收到任何错误,所以我不知道我的代码有什么问题。当我在浏览器中打开 index.html 时,我没有看到分数。

index.html:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>My HTML File</title>
  <link rel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script 
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"> 
  </script>
  <script 
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.js"></script>
  <script src = "app.js"></script>
 </head>
 <body ng-app = "app">
  <p ng-controller="ScoreController">
     Score: {{score.points}}
  </p>
  <p ng-controller="ScoreController">
     <button ng-click="increment()">Increment</button>
  </p>
 </body>
</html>

app.js:

angular.module('app', [])
 .value('randomScore', function(){
   return Math.ceil(Math.random() * 10);
});

angular.module('app').controller('ScoreController', ['randomScore', function 
 ($scope, score, randomScore){
   $scope.score = score;
   $scope.increment = function(){
   $scope.score.points += randomScore();
        };
}]);

index.html-截图 !image1

原来我缺少分数构造器和分数服务:

得分-constructor.js:

function Score(randomScore){
this.points = randomScore();
   }

得分-service.js:

angular.module('app')
.service('score', Score);

添加这 2 个文件并解决@Aleksey Solovey 在评论中指出的问题解决了这个问题。

试试这个

angular.module('app').controller('ScoreController',['$scope','score','randomScore',function($scope, score, randomScore){
   $scope.score = score;
   $scope.increment = function(){
           $scope.score.points += randomScore();
        };
}]);