Access to $http inside DocView?

Hi there,

I'm trying to connect to a REST API out of a DocumentView in Kibana, cause I try to implement the functionality that developers are able to open tickets in our ticket system, when they are looking into the application logs. But I'm a newbie regarding Angular and therefore a little bit stuck trying to call the $http-service. I always get an error when trying to access $http. The code in the *.js is the following:

import _ from 'lodash';
import angular from 'angular';
import { DocViewsRegistryProvider } from 'ui/registry/doc_views';
import templateHtml from './template.html';

DocViewsRegistryProvider.register(function() {
  return {
    title: 'Open ticket',
    order: 5,
    directive: {
      template: templateHtml,
      scope: {
        hit: '=',
        indexPattern: '='
      },
      controller: function( $scope ) {
        ........

        $scope.openTicket = function( $scope ) {
          $http.post('<URL>', '<TESTJSON>' ).
            then(function(response) {
                .....
            }, function(response) {
                .....
            });
        };
      }
    }
  };
});

This results in the following error during the call of openTicket():

angular.js:14642 TypeError: Cannot read property 'post' of undefined
at Scope.$scope.openTicket (email.js:42)
at fn (eval at compile (angular.js:15500), :4:150)
at callback (angular.js:27285)
at Scope.$eval (angular.js:18372)
at Scope.$apply (angular.js:18472)
at HTMLFormElement. (angular.js:27290)
at HTMLFormElement.dispatch (jquery.js:4737)
at HTMLFormElement.elemData.handle (jquery.js:4549)

Can anybody help me, how I can make the HTTP-Service visible in function inside the controller?

Hey Markus,

$http is an Angular service, that you can inject into your service.
You must basically just add it as a parameter to the function you pass to register and Angular will inject that service.

So your line 6 should look as follows:

DocViewsRegistryProvider.register(function($http) {

Cheers,
Tim

1 Like

Thank you very much, Tim. :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.