2015-04-14 22 views
1

我想用$ HTTP來發送Ajax請求,我寫這樣的代碼,但我得到的錯誤

Error: $http is not defined 

我把這個代碼在運行

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

}])

+0

我不清楚你想do..please精心更多 –

+0

@pankajparkar我想在運行 –

+0

發送Ajax請求,爲什麼你想在運行程序段正是 –

回答

2

你有在控制器中定義$http變量。

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

這樣的事情。在運行方法中嘗試這樣的事情。

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

我想在運行中使用它併發送ajax請求,而不是在控制器中運行 –

+0

我已更新我的帖子。 – Stony

+0

我寫的完全是這樣的,但它不適用於我 –

相關問題