0

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?

4
  • 1
    Where is the controller code, from where you call that function? Commented Nov 9, 2015 at 7:44
  • did you tried this to pass those as the parameters? controller: ["$scope", "$http", CompletedActionsCtrl($scope, $http)] Commented Nov 9, 2015 at 12:44
  • yes. it doesn't work. Commented Nov 10, 2015 at 17:17
  • follow this link might be helpful for you, it passes apitoken argument to another one : docs.angularjs.org/guide/providers#service-recipe Commented Nov 17, 2015 at 14:28

2 Answers 2

1

Remove $http from your function, the parameter which you have added on your addCommentHandler function is vanishing the existence $http service.

Code

function addCommentHandler(params){ //<-- removed $http paramter function.
    var url = "./postComment";
    $http({method: 'GET',url: url
  })
  .then(function successCallback(response) {}, 
     function errorCallback(response) {});
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But i am still getting the error. Uncaught ReferenceError: $http is not defined
1

Remove $http from method declaration, you are trying to inject a angular service to a private method!

6 Comments

Thanks. But i am still getting the error. Uncaught ReferenceError: $http is not defined
to use $http angularjs service you need to inject it into controller under this method is used, show me you controller code please ?
Thanks! Added it in the Qs. plz see the code under //==Controller invoker== and Code Flow Explanation.
I think your issue here is the way you did your controller, CompletedActionsCtrl have no idea what $http is, check plz this answer how to declare a controller, then in [] of controller you can inject a angularjs services like $http and use them in your function.
But i am injecting $http in CompletedActionsCtrl. Also, if i use $http inside this controller then also it works fine. Problem is i can't use $http inside a private function(addCommentHandler in this case) called from CompletedActionsCtrl.
|

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.