admin管理员组文章数量:1420918
I have the following angular code:
'use strict';
/**
* @ngdoc service
* @name .cordova
* @description
* # cordova
* Factory in the APP.
*/
angular.module('app')
.factory('cordova', function () {
// Service logic
// ...
var d = $q.defer(),
resolved = false;
var self = this;
this.ready = d.promise;
document.addEventListener('deviceready', function () {
resolved = true;
d.resolve($window.cordova);
});
// Check to make sure we didn't miss the
// event (just in case)
setTimeout(function () {
if (!resolved) {
if ($window.cordova) d.resolve($window.cordova);
}
}, 3000);
// Public API here
return this;
/*var meaningOfLife = 42;
// Public API here
return {
someMethod: function () {
return meaningOfLife;
}
};*/
});
I have the following angular code:
'use strict';
/**
* @ngdoc service
* @name .cordova
* @description
* # cordova
* Factory in the APP.
*/
angular.module('app')
.factory('cordova', function () {
// Service logic
// ...
var d = $q.defer(),
resolved = false;
var self = this;
this.ready = d.promise;
document.addEventListener('deviceready', function () {
resolved = true;
d.resolve($window.cordova);
});
// Check to make sure we didn't miss the
// event (just in case)
setTimeout(function () {
if (!resolved) {
if ($window.cordova) d.resolve($window.cordova);
}
}, 3000);
// Public API here
return this;
/*var meaningOfLife = 42;
// Public API here
return {
someMethod: function () {
return meaningOfLife;
}
};*/
});
But I get the following error:
ReferenceError: $q is not defined
at Object.<anonymous> (http://localhost:9000/scripts/services/cordova.js:15:13)
at Object.invoke (http://localhost:9000/bower_ponents/angular/angular.js:4625:19)
at Object.enforcedReturnValue [as $get] (http://localhost:9000/bower_ponents/angular/angular.js:4464:37)
at Object.invoke (http://localhost:9000/bower_ponents/angular/angular.js:4625:19)
at http://localhost:9000/bower_ponents/angular/angular.js:4424:37
at getService (http://localhost:9000/bower_ponents/angular/angular.js:4571:39)
at injectionArgs (http://localhost:9000/bower_ponents/angular/angular.js:4595:58)
at Object.instantiate (http://localhost:9000/bower_ponents/angular/angular.js:4637:18)
at $controller (http://localhost:9000/bower_ponents/angular/angular.js:10042:28)
at link (http://localhost:9000/bower_ponents/angular-route/angular-route.js:1007:26) <div ng-view="" class="ng-scope">
And here is my index.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1"
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<!-- build:css(.) styles/vendor.css -->
<!-- bower:css -->
<link rel="stylesheet" href="bower_ponents/bootstrap/dist/css/bootstrap.css" />
<!-- endbower -->
<!-- endbuild -->
<!-- build:css(.tmp) styles/main.css -->
<link rel="stylesheet" href="styles/main.css">
<!-- endbuild -->
</head>
<body ng-app="app">
<div ng-view></div>
<script type="text/javascript" src="cordova.js"></script>
<!-- build:js(.) scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_ponents/jquery/dist/jquery.js"></script>
<script src="bower_ponents/angular/angular.js"></script>
<script src="bower_ponents/bootstrap/dist/js/bootstrap.js"></script>
<script src="bower_ponents/angular-animate/angular-animate.js"></script>
<script src="bower_ponents/angular-cookies/angular-cookies.js"></script>
<script src="bower_ponents/angular-resource/angular-resource.js"></script>
<script src="bower_ponents/angular-route/angular-route.js"></script>
<script src="bower_ponents/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_ponents/angular-touch/angular-touch.js"></script>
<!-- endbower -->
<!-- endbuild -->
<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="scripts/controllers/about.js"></script>
<script src="scripts/services/cordova.js"></script>
<!-- endbuild -->
</body>
</html>
Share
Improve this question
edited Apr 18, 2016 at 6:58
Sotiris Kiritsis
3,3563 gold badges24 silver badges31 bronze badges
asked Apr 18, 2016 at 6:30
SmithaSmitha
6,12424 gold badges92 silver badges164 bronze badges
2
-
1
.factory('cordova', function ($q) {
– Arun P Johny Commented Apr 18, 2016 at 6:32 - Possible duplicate of AngularJs console.log "$q is not defined" – Shanimal Commented Apr 18, 2016 at 6:34
1 Answer
Reset to default 6You have to inject $q
in yout factory
angular.module('app').factory('cordova', function ($q) {
// Some code...
});
本文标签: javascriptq is undefinedReference errorStack Overflow
版权声明:本文标题:javascript - $q is undefined - Reference error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745344864a2654431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论