简体   繁体   English

如何在angularjs中使用$ http?

[英]how to use $http in run in angularjs?

I want to use $http for send ajax request , I write this code but i get error 我想使用$ http来发送ajax请求,我编写了这段代码,但出现错误

Error: $http is not defined

I put this code in run 我将这段代码运行

app.run(['$rootScope','$location','$timeout','$http', function ($rootScope,$location,$timeout,$http) {
    $rootScope.getMember = function(val){
    var persons = $rootScope.selectPerson;
    if(persons.length == 0)
        persons = null;
    return $http({
        url: 'projectTask/GetMemberForFreeTask',
        method: "POST",
        data: $.param({'searchStr':val,'selectedPerson': persons}),
       headers: {'Content-Type': 'application/x-www-form-urlencoded'}   
    })
    .then(function(response) {
        for(var i = 0; i<response.data.length; i++){
            response.data[i]['first_name'] = response.data[i]['first_name']+' '+ response.data[i]['last_name'] 
        }
        $rootScope.states = response.data;
            return response.data
        }
    );
}

}]) }])

You have to define the $http variable in your controller. 您必须在控制器中定义$http变量。

app.controller('TestController', function TestController($scope, $http) {
}

something like this. 这样的事情。 In the run method try something like this. 在run方法中,尝试类似这样的操作。

angular.module('yourApp').run(['$rootScope', '$http', function ($rootScope, $http) {
  // do stuff here
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM