Angularjs service method not getting called, TypeError: Cannot read property '
Angularjs service method not getting called, TypeError: Cannot read property '
我是 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');
});*/
};
我创建了一个基本服务,只是 return 将名称作为参数传递给它。
这是我的服务(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);
我已经检查了很多次语法和如何创建服务,但仍然无法找出原因。请帮我解决这个问题。
在您的控制器中,您没有正确注入 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);
}
我是 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');
});*/
};
我创建了一个基本服务,只是 return 将名称作为参数传递给它。 这是我的服务(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);
我已经检查了很多次语法和如何创建服务,但仍然无法找出原因。请帮我解决这个问题。
在您的控制器中,您没有正确注入 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);
}