0

我被这段代码困住了很长时间,并且应用了所有可用的网络补丁,但是没有找到有效的补丁。它仍然在从控制器调用服务时发生错误。 这里下面未知的提供者:callbackProvider < - 回调

<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> 

回答

0

代码我已经重写它像这样:

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" 
     }); 
    } 
    } 
}); 
相关问题