[英]Ionic call function when app is loaded
I would like to check every time the app is loaded if the device has an internet connection. 我想在每次加载应用程序时检查设备是否可以连接互联网。 This function has nothing to do with any of my Ionic Views/pages. 此功能与我的任何“离子视图” /页面无关。 I don't need to make this check on a specific page. 我不需要在特定页面上进行此检查。 I need it to run every time the app is actually loaded on the screen. 每当应用实际在屏幕上加载时,我都需要运行它。
For example, if I hit the app icon from my home screen and load it, the check function should run. 例如,如果我从主屏幕上点击了应用程序图标并加载了它,则检查功能应运行。 Then I decide to open my emails to check on something. 然后,我决定打开我的电子邮件以检查某些内容。 At the end, I just re-open my app which is running in the background (Not terminated), the app should check again for an internet connection. 最后,我只是重新打开在后台运行的应用程序(未终止),该应用程序应再次检查互联网连接。
Im not asking how I'm I gonna write the function to check, but where shall I place that segment of code to run only when the app is loaded regardless the view/page of the app. 我不是在问我如何编写要检查的功能,而是我应该将这段代码放置在什么位置,以便仅在加载应用程序时运行,而不管应用程序的视图/页面如何。
For the first check, when you start your app, add your check code inside $ionicPlatform.ready method (app.js file). 对于第一次检查,在启动应用程序时,请将检查代码添加到$ ionicPlatform.ready方法(app.js文件)中。
For the check when you return to app from other activity, create inside .run method (app.js file) the $ionicPlatform.on method. 对于从其他活动返回到应用程序时的检查,请在.run方法(app.js文件)内部创建$ ionicPlatform.on方法。
Something like this (at app.js file): 这样的事情(在app.js文件中):
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
// PUT YOUR CHECK CODE HERE, TRIGGERED WHEN THE APP RUN FOR THE FIRST TIME
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
$ionicPlatform.on('resume', function() {
// PUT YOUR CHECK CODE HERE TOO. TRIGGERED WHEN YOU RETURN TO APP FROM OTHER ACTIVITY.
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.