I am getting the very common device is not defined
error when I load my Ionic application in both the browser (via ionic serve
) and on my device.
I am new to TypeScript and I think my trouble may be related to some misunderstandings there.
My index.html
contains (note that ng-app
is missing from my DOM element):
<link href="css/ionic.app.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- inject:js -->
<script src="lib/node_modules/ng-cordova/dist/ng-cordova-mocks.min.js"></script>
<script src="lib/node_modules/ng-cordova/dist/ng-cordova.min.js"></script>
<script src="lib/node_modules/ng-cordova/src/plugins/device.js"></script>
<script src="lib/node_modules/ng-cordova/src/plugins/file.js"></script>
<!-- endinject -->
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
</head>
<body>
<div ui-view=""></div>
<script src="app.js"></script>
<script src="bootstrapApp.js"></script>
</body>
Here, app.js
is compiled from TypeScript. I have quite a bit going on in here, but the interesting part is this:
var App;
(function (App) {
"use strict";
angular.module("app", [
"ionic",
"ngCordova",
"ui.router"
])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider.state("splash", {
url: "/splash",
templateUrl: "components/splash/splash.html",
controller: "SplashController"
});
$urlRouterProvider.otherwise("/splash");
});
})(App || (App = {}));
var App;
(function (App) {
"use strict";
var DeviceService = (function () {
function DeviceService($cordovaDevice) {
this.device = null;
this.device = $cordovaDevice.getDevice();
console.log(this.device);
}
DeviceService.$inject = ["$cordovaDevice"];
return DeviceService;
})();
angular.module("app").service("DeviceService", DeviceService);
})(App || (App = {}));
To ensure that my device is "ready", I followed a suggestion I found here: https://stackoverflow.com/a/27975700/3817101
So, I have a bootstrapApp.js
file that contains:
window.ionic.Platform.ready(function() {
console.log("platform is ready");
angular.bootstrap(document, ['app']);
});
On page load, everything appears to be happy, except for the use of $cordovaDevice
. My console reads:
platform is ready
ionic.bundle.js:20306 ReferenceError: device is not defined
It's obvious that I'm attempting to use the device before things are "ready" but I can't seem to figure out where I'm going wrong. Any suggestions?