1

I want to use $http for send ajax request , I write this code but i get error

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
        }
    );
}

}])

5
  • I'm not clear what exactly you wanted to do..please elaborate more Commented Apr 14, 2015 at 10:12
  • @pankajparkar I want to send ajax request in run Commented Apr 14, 2015 at 10:15
  • why you want it in run block Commented Apr 14, 2015 at 10:19
  • If I were you, I would think very hard about why you want to code something in the Run phase of the angular application. If you set up routes then you can have your first route to initialise anything in particular. Commented Apr 14, 2015 at 10:24
  • because i want to declare three rootscope function to use it in all controllers Commented Apr 14, 2015 at 10:26

1 Answer 1

2

You have to define the $http variable in your controller.

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

something like this. In the run method try something like this.

angular.module('yourApp').run(['$rootScope', '$http', function ($rootScope, $http) {
  // do stuff here
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

i want to use it in run and send ajax request in run not in a controller
I had a similar issue and adding the variable in the run parameters worked for me. I used it like this angular.module('yourApp').run(function ($rootScope, $http) {...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.