服务不适用于简单的控制器

Aut*_*cus -1 angularjs angularjs-directive angularjs-service angularjs-scope

我有这个js代码

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


app.service('testService', function(){
  this.sayHello= function(text){
    return "Service says \"Hello " + text + "\"";
  };
  this.sayGoodbye = function(text){
    return "Service says \"Goodbye " + text + "\"";
  };
});


app.controller('AboutCtrl', ['testService', function ($scope, $location, $http) {


  $scope.fromService = testService.sayHello("World");
  $scope.toService = testService.sayGoodbye("World");
}]);
Run Code Online (Sandbox Code Playgroud)

在我的HTML中我有这个.... ...嗨{{fromService}} .... ...控制台中没有错误,页面只是空白.

Ism*_*OUH 5

请查看AngularJs Docs"使用依赖注入".

正确的方法:

app.controller('AboutCtrl', ['$scope', '$location', '$http', 
'testService', function ($scope, $location, $http, testService) {
        $scope.fromService = testService.sayHello("World");
        $scope.toService = testService.sayGoodbye("World");
}]);
Run Code Online (Sandbox Code Playgroud)