我有以下角度代码:
'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;
}
};*/
});
但是我收到以下错误:
ReferenceError: $q is not defined
at Object.<anonymous> (http://localhost:9000/scripts/services/cordova.js:15:13)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4625:19)
at Object.enforcedReturnValue [as $get] (http://localhost:9000/bower_components/angular/angular.js:4464:37)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4625:19)
at http://localhost:9000/bower_components/angular/angular.js:4424:37
at getService (http://localhost:9000/bower_components/angular/angular.js:4571:39)
at injectionArgs (http://localhost:9000/bower_components/angular/angular.js:4595:58)
at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:4637:18)
at $controller (http://localhost:9000/bower_components/angular/angular.js:10042:28)
at link (http://localhost:9000/bower_components/angular-route/angular-route.js:1007:26) <div ng-view="" class="ng-scope">
这是我的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_components/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_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script src="bower_components/angular-animate/angular-animate.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/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>
答案 0 :(得分:6)
你必须在你的工厂注入$q
angular.module('app').factory('cordova', function ($q) {
// Some code...
});