$ http不是指令的链接函数中的函数

use*_*349 2 javascript angularjs angularjs-directive angular-http

我是角度js的新手,并试图使用指令.我想在指令的link函数中使用$ http.以下是我的代码

MyApp.directive('appItem', ['$http',function() { 
  return { 
    restrict: 'E', 
    scope: { 
      transaction: '=' 
    }, 
    templateUrl: 'js/directives/appItem.html' ,

    link: function($scope, $http, element, attrs) {
            $scope.process = function(trId) {
               $http({
                    method: 'PATCH',
                    url: 'http://myapp.domain.local/api/v1/items/'+trId+'.json'
                }).
               then(function successCallback(response) {
                console.log(response);
               });
            }
        }

  }
}]);
Run Code Online (Sandbox Code Playgroud)

但它给了我错误:

$http is not a function
Run Code Online (Sandbox Code Playgroud)

小智 7

MyApp.directive('appItem', ['$http',function($http) { 
  return { 
    restrict: 'E', 
    scope: { 
      transaction: '=' 
    }, 
    templateUrl: 'js/directives/appItem.html' ,

    link: function(scope,element, attrs) {
            scope.process = function(trId) {
               $http({
                    method: 'PATCH',
                    url: 'http://myapp.domain.local/api/v1/items/'+trId+'.json'
                }).
               then(function successCallback(response) {
                console.log(response);
               });
            }
        }

  }
}]);
Run Code Online (Sandbox Code Playgroud)