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...
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745344865a4623496.html
评论列表(0条)