title: AngularJS : how to call service function from controller
body: <p>AngularJS : a web socket defines several functions inside a service.
  Do you know how to call these functions (onCLose, send, ...) from a controller ??
  The controller is called when i click on a button.
  A error says "is not a function" about these 2 lines :
  $scope.websocket.send  and  $scope.websocket.onClose</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;&lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;script type="text/javascript" src="lib/angular.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="lib/angular-websocket.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
  var app = angular.module('myApp', ['ngWebSocket']);
  
  app.service('WebSocketWrapper', ['$log', '$websocket', '$rootScope', function($log, $websocket, $rootScope){
      var ws = null;
      this.state = 'initializing';
      this.message = 'websocket initializing';
      var self = this;
      this.init = function(){
          if(!ws){
              ws = $websocket('ws://127.0.0.1:8080/WebSocketHome/echo', null, {reconnectIfNorNormalClose: true});
  
              ws.onClose(function(){
                  console.info('close');
                  $rootScope.$apply(function(){
                      self.state = 'disconnected';
                      self.message = 'Websocket disconnected';
                  });
              });
  
              ws.onOpen(function(){
                  console.info('connected');
                  $rootScope.$apply(function(){
                      self.state = 'connected';
                      self.message = 'websocket connected';
                  });
              });
  
              ws.onMessage(function(message){
                  console.log("RECEIVED : " + message);
              });
          }
      };
  }]);
  
  app.controller('WebSocketStateCtrl', ['$scope', 'WebSocketWrapper', function($scope, WebSocketWrapper){
      $scope.websocket = WebSocketWrapper;
      $scope.websocket.init();
      $scope.sendMessage = function(){
          $scope.websocket.send($scope.name);
          $scope.websocket.onClose();
      }
  
  }]);
  
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;div ng-app="myApp" ng-controller="WebSocketStateCtrl"&gt;
          &lt;form action=""&gt;
              &lt;input type="text" ng-model="name"&gt;
              &lt;button ng-click="sendMessage()"&gt;SEND&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>