I am using Angular Grid (ag-grid) to display data. In my table, whenever user adds a comment by inline editing, i intend to make a rest call and save comment in DB. I have put $http
thing to make rest call in newValueHandler
. In debugger i see that newValueHandler
is invokded but $http
is undefined. And then i get the error: Uncaught TypeError: $http is not a function
function CompletedActionsCtrl($scope, $http) {
var columnDefs = [{
headerName: "Comments",
field: "comments",
width: 180,
filter: 'text',
editable: true,
newValueHandler:addCommentHandler,
valueGetter: function (params) {
if (params.data.comments != null) {
return params.data.comments[0];
}
}
}]
function addCommentHandler(params,$http){
var url = "./postComment";
$http({method: 'GET',url: url
}).then(function successCallback(response) {}, function errorCallback(response) {});}
//==Controller invoker==
var app = angular.module('app', ['ngRoute', 'agGrid']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/completed_actions', {
templateUrl: 'completed_actions_partial.html',
controller: CompletedActionsCtrl
});
}]);
Seems to be a trivial task, and i am sure i am missing something basic.
P.S. New to Javascript and angularjs.
== Code Flow Explanation ==
When user hits completed actions tab, CompletedActionsCtrl
is invoked by routing. Inside this controller, a grid is prepared and rendered. Now when user edits the comment, and submits it, newValueHandler
is invoked which is function addCommentHandler
== Update == Got it working by instead of calling a private function as handler, defined an anonymous function.
So removed this ->
newValueHandler:addCommentHandler,
And Added this ->
newValueHandler:function(params){
var url = "./postComment";
$http({method: 'GET',url: url})
.then(function successCallback(response) {},
function errorCallback(response) {});},
But the problem is that i need to have this handler at many places, and this approach will cause lot of code duplication. Any better ideas?