未调用Angularjs服务方法,TypeError:无法读取属性'
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');
});*/
};
我创建了一个基本服务,只返回作为参数传递给它的名称。这是我的服务(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);
}
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- JavaScript承诺-无法读取属性'那么'的未定义
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- http/rxjs catch回调中的Angular 2重定向导致TypeError:无法读取属性'订阅'
- Javascript未捕获类型错误:无法读取属性'0'的未定义
- jQuery控制台错误:无法读取属性'top'的未定义
- TypeError:无法读取属性'findAll'的未定义