Angularjs服务方法没有被调用,TypeError:无法读取属性'

时间:2015-12-04 11:58:16

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-service

我是angularJs的新手。 我在从服务调用方法时遇到问题。

这是我的控制器(app.js)

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 && 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');
 });*/

};

我创建了一个基本服务,只返回传递给它的名称作为参数。 这是我的服务(services.js)

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;
}
});

最后在我的视图(Index.html)中,我只是调用该函数并使用一些控制台语句显示内容。

<button class="button button-bar button-balanced" ng-click="doContactPicker()">Pick Contact</button>

现在我得到的错误是: -

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 <anonymous> (http://192.168.0.126:8100/lib/ionic/js/ionic.bundle.js:21972:15), <anonymous>: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.<anonymous> (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
错误陈述顶部的

未定义是

console.log(getName);

我已经检查了语法以及如何多次创建服务,但仍然无法找出原因。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

在您的控制器中,您没有正确注入getName服务

以下工作

.controller('SmsCtrl', ['$scope','$ionicPlatform','$cordovaSms', 'getName', function ($scope, $ionicPlatform, $cordovaSms, getName)

答案 1 :(得分:1)

您必须在要使用它的控制器中按名称注入您的服务,并将此名称用作对象

.controller('SmsCtrl', ['$scope','$ionicPlatform','$cordovaSms','getName', function ($scope, $ionicPlatform, $cordovaSms, getName) {
    console.log(getName);
}