未知提供者:callbackProvider <- callback

Unknown provider: callbackProvider <- callback

我在这段代码上坚持了很长时间,并应用了网上所有可用的补丁,但没有找到有效的 one.It 从控制器调用服务时仍然出错。 下面是代码

<HTML ng-app="myApp">
<body ng-controller = "myCtrl">
    <div>{{me}}</div>
</body>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
    <script>

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

        app.controller('myCtrl',function($scope,myService){
            myService.getx(function(data){
            console.log(data);
            $scope.me = "data"; 
            });

        });

        </script>
        <script>
        app.service('myService',function($http,callback){
        this.getx= function(){
                 return $http({
                method: "GET",

                url: "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js"
              }).then(function (response) {
                      console.log(response)
                     return callback(response);
                 }, function (error) {
                     throw error;
                     console.log("Error",error)
                 });

        }

        });

    </script>
</HTML>

我会这样重写:

APP CTRL:

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

 app.controller('myCtrl',function($scope,myService){
   myService.getx()
     .then(
     function(data){ //handle the $http promise here
       console.log(data);
       $scope.me = "data"; 
     },
     function(err){
       console.log('error:' + err);
     });

 });

服务:

app.service('myService',function($http){
  return {
    getx: function() {  
     return $http({  //the $http returns a promise
       method: "GET",
       url:"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js"
       });
    }
  }
});