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

Angularjs service method not getting called, TypeError: Cannot read property '

本文关键字:读取 属性 Angularjs 调用 服务 方法 TypeError      更新时间:2023-09-26

我是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

错误语句顶部的undefined用于

console.log(getName);

我已经检查了很多次语法和如何创建服务,但仍然无法找出原因。请帮我弄清楚。

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

以下将工作

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

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

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