title: Angularjs service method not getting called, TypeError: Cannot read property '
body: <p>I am new to angularJs.
  I am havin problem in calling a method from service.</p>
  
  <p>Here is my controller (app.js)</p>
  
  <pre><code>angular.module('starter', ['ionic','ngCordova','NameService'])
  
  .run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if(window.cordova &amp;&amp; window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      }
      if(window.StatusBar) {
        StatusBar.styleDefault();
      }
    });
  })
  .controller('SmsCtrl', ['$scope','$ionicPlatform','$cordovaSms', function ($scope, $ionicPlatform, $cordovaSms, getName) {
    console.log('enetered in ctrl');
    $scope.form={}
  $scope.counter=contactCount;
  
  $scope.doContactPicker=function() {
  console.log(getName);
  //$scope.answer='answer';
  $scope.answer =getName.nameFetched('yatin data');
  
   /*$scope.$watch('answer', function() {
          document.getElementById("contactFetched").innerHTML =$scope.answer;
  alert('sample alert displayed');
   });*/
  
  };
  </code></pre>
  
  <p>I have created a basic service to just return the name passed to it as argument.
  Here is my service(services.js)</p>
  
  <pre><code>var nameService=angular.module('NameService',[]);
  
  
  
  nameService.service('getName',function() {
  console.log("service created");
   this.nameFetched=function getUserName(c) {
  console.log("inside picked contact");
      var name =c; 
      return name;
  }
  });
  </code></pre>
  
  <p>And finally in my view(Index.html), I am just calling the function and displaying the content with some console statements.</p>
  
  <pre><code>&lt;button class="button button-bar button-balanced" ng-click="doContactPicker()"&gt;Pick Contact&lt;/button&gt;
  </code></pre>
  
  <p>Now the error I am getting is :-</p>
  
  <pre><code>undefined
  
  ionic.bundle.js:21157 TypeError: Cannot read property 'nameFetched' of undefined
      at Scope.$scope.doContactPicker (http://192.168.0.126:8100/js/app.js:30:23)
      at fn (eval at &lt;anonymous&gt; (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:21972:15), &lt;anonymous&gt;:4:236)
      at http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:57514:9
      at Scope.parent.$get.Scope.$eval (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:24673:28)
      at Scope.parent.$get.Scope.$apply (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:24772:23)
      at HTMLButtonElement.&lt;anonymous&gt; (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:57513:13)
      at HTMLButtonElement.eventHandler (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:12098:21)
      at triggerMouseEvent (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:2865:7)
      at tapClick (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:2854:3)
      at HTMLDocument.tapMouseUp (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:2927:5)(anonymous function) @ ionic.bundle.js:21157ident.$get @ ionic.bundle.js:17936parent.$get.Scope.$apply @ ionic.bundle.js:24774(anonymous function) @ ionic.bundle.js:57513eventHandler @ ionic.bundle.js:12098triggerMouseEvent @ ionic.bundle.js:2865tapClick @ ionic.bundle.js:2854tapMouseUp @ ionic.bundle.js:2927
  </code></pre>
  
  <p>undefined at the top of the error statements is for </p>
  
  <pre><code>console.log(getName);
  </code></pre>
  
  <p>I have checked the syntax and how to create services many times now and still not able to figure out the reason for this. Kindly help me figure this out.</p>