我正在尝试使用Cordova设备插件来获取有关用户正在使用的设备的信息。 文档说它只能在设备deviceready函数之后调用,所以在我的.run函数中我有以下内容:
.run(function($rootScope, $state, $stateParams) { document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { console.log(device); } //more functions below这工作得很好,我在控制台中看到了设备对象,但我真正想要的是在工厂内部使用设备对象,有时会在deviceready事件之前调用deviceready 。
所以我的问题是,如何让设备对象在工厂可用时可用? 我想要下面的代码
angular.module('myapp.services.myFactory', []) .factory('MyFactory', function($q) { function getDevice () { //if the deviceready event happend and I can access the device object set to variable return device; } return { getDeviceInfo: function getDeviceInfo() { return $q(function(resolve, reject) { if (getDevice()) { //do something with the device object } else { //do something without device object } }); } }; });I am trying to use the Cordova Device plugin to get info about the device the user is using. The docs say it can only be called AFTER the deviceready function, so in my .run function I have the following:
.run(function($rootScope, $state, $stateParams) { document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { console.log(device); } //more functions belowThis is working great, I see the device object in the console, however what I really want is to use the device object inside a factory that will sometimes get called BEFORE the deviceready event.
So my question is how can I get make device object available to my factory when it becomes available? I want something like the below code
angular.module('myapp.services.myFactory', []) .factory('MyFactory', function($q) { function getDevice () { //if the deviceready event happend and I can access the device object set to variable return device; } return { getDeviceInfo: function getDeviceInfo() { return $q(function(resolve, reject) { if (getDevice()) { //do something with the device object } else { //do something without device object } }); } }; });最满意答案
无需在这里使用。运行 - 您应该能够执行deviceready内的所有内容,包括模块和工厂定义。 只是把这个事件全部包起来。 我也不相信你需要$q来解决任何承诺。 观察以下简化模式...
function onDeviceReady() { // guessing your dependency architecture angular .module('myapp', ['myapp.services']) .module('myapp.services', ['myapp.services.myFactory']) .module('myapp.services.myFactory', []) .factory('MyFactory', function() { function withDevice() { /*...*/ } function withoutDevice() { /*...*/ } return { // turnary. device always available to examine at this point 'getDeviceInfo': device ? withDevice : withoutDevice } }); } document.addEventListener('deviceready', onDeviceReady, false);No need to leverage .run here - you should be able to execute everything inside deviceready including your module and factory definitions. Just wrap everything in with this event. I also don't believe you need $q to resolve any promises either. Observe the following simplified pattern...
function onDeviceReady() { // guessing your dependency architecture angular .module('myapp', ['myapp.services']) .module('myapp.services', ['myapp.services.myFactory']) .module('myapp.services.myFactory', []) .factory('MyFactory', function() { function withDevice() { /*...*/ } function withoutDevice() { /*...*/ } return { // turnary. device always available to examine at this point 'getDeviceInfo': device ? withDevice : withoutDevice } }); } document.addEventListener('deviceready', onDeviceReady, false);更多推荐
发布评论