AngularJS / Cordova:如何在设备准备完成后在工厂获取值?(AngularJS/Cordova: How can I get values in my factory after de

编程入门 行业动态 更新时间:2024-10-25 17:19:30
AngularJS / Cordova:如何在设备准备完成后在工厂获取值?(AngularJS/Cordova: How can I get values in my factory after deviceready?)

我正在尝试使用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 below

This 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);

更多推荐

本文发布于:2023-08-06 10:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工厂   完成后   设备   如何在   Cordova

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!