programing

AngularJs ReferenceError : $ http가 정의되지 않았습니다

new-time 2020. 5. 10. 13:40
반응형

AngularJs ReferenceError : $ http가 정의되지 않았습니다


다음과 같은 각도 함수가 있습니다.

$scope.updateStatus = function(user) {    
    $http({
        url: user.update_path, 
        method: "POST",
        data: {user_id: user.id, draft: true}
    });
};

그러나이 함수가 호출 될 때마다

ReferenceError: $http is not defined

콘솔에 들어갑니다. 누군가 내가 여기서 뭘 잘못하고 있는지 이해하도록 도울 수 있습니까?


 

$http

컨트롤러에 서비스를 주입하지 않았을 수 있습니다 . 여러 가지 방법이 있습니다.

DI에 대한이 참조를

읽으십시오 . 그런 다음 매우 간단 해집니다.

function MyController($scope, $http) {
   // ... your code
}

사용할 때 같은 문제를 겪었습니다.

    myApp.controller('mainController', ['$scope', function($scope,) {
        //$http was not working in this
    }]);

위 코드를 아래와 같이 변경했습니다. 아래와 같이 $ http (2 회)를 포함해야합니다.

 myApp.controller('mainController', ['$scope','$http', function($scope,$http) {
      //$http is working in this
 }]);

그리고 그것은 잘 작동했습니다.


 

Amit Garg 답변

을 완료 하기 위해 AngularJS에 종속성을 주입하는 몇 가지 방법이 있습니다.


 

$inject

종속성을 추가하는 데 사용할 수도 있습니다 .

var MyController = function($scope, $http) {
  // ...
}
MyController.$inject = ['$scope', '$http'];

참고 URL :

https://stackoverflow.com/questions/13759120/angularjs-referenceerror-http-is-not-defined

반응형