在angularjs中执行函数后执行代码

Posted

技术标签:

【中文标题】在angularjs中执行函数后执行代码【英文标题】:Execute code after function execution in angularjs 【发布时间】:2015-08-28 15:48:31 【问题描述】:

我正在使用带有 SQLite 的 Phonegap 为 ios 制作应用程序。 在 deviceready() 中,我从 JSON 文件加载 SQLite 表。

代码:

document.addEventListener("deviceready", onDeviceReady, false);

var db;

function onDeviceReady() 
  try
  
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 2048*1024, FileIO.gotFS, FileIO.errorHandler);

        db = window.sqlitePlugin.openDatabase( name: "ValeDB", location: 1 );

        db.transaction(function(tx) 

                       tx.executeSql('DROP TABLE IF EXISTS tbl_Location');
                       tx.executeSql('DROP TABLE IF EXISTS tbl_OrgUnit');
                       tx.executeSql('CREATE TABLE IF NOT EXISTS tbl_Location (id integer, value text)');
                       tx.executeSql('CREATE TABLE IF NOT EXISTS tbl_OrgUnit (id integer, value text, type text)');

                       LocationList.forEach(function(loc)
                                            //console.log(loc.value);
                                            tx.executeSql("INSERT INTO tbl_Location (id, value) VALUES (?, ?)", [loc.id, loc.value],
                                                          function(tx, res)
                                                            //console.log("Success: " + res.insertId);
                                                          ,
                                                          function(e)
                                                            console.log("Error: " + e.message);
                                                          );
                                            );

                       tx.executeSql("SELECT COUNT(*) AS Count FROM tbl_Location", [], function(tx, res)
                                     if(res.rows.length > 0)
                                        console.log("New rows added to tbl_Location:" + res.rows.item(0).Count);
                                     );

                       EnglishOrgUnit.forEach(function(org)
                                            //console.log(loc.value);
                                            tx.executeSql("INSERT INTO tbl_OrgUnit (id, value, type) VALUES (?, ?, ?)", [org.id, org.value, "en"],
                                                          function(tx, res)
                                                          //console.log("Success: " + res.insertId);
                                                          ,
                                                          function(e)
                                                          console.log("Error: " + e.message);
                                                          );
                                            );

                       tx.executeSql("SELECT COUNT(*) AS Count FROM tbl_OrgUnit WHERE TYPE = 'en'", [], function(tx, res)
                                     if(res.rows.length > 0)
                                     console.log("New Eng. rows added to tbl_OrgUnit:" + res.rows.item(0).Count);
                                     );

                       PortugueseOrgUnit.forEach(function(org)
                                              //console.log(loc.value);
                                              tx.executeSql("INSERT INTO tbl_OrgUnit (id, value, type) VALUES (?, ?, ?)", [org.id, org.value, "pt"],
                                                            function(tx, res)
                                                            //console.log("Success: " + res.insertId);
                                                            ,
                                                            function(e)
                                                            console.log("Error: " + e.message);
                                                            );
                                              );

                       tx.executeSql("SELECT COUNT(*) AS Count FROM tbl_OrgUnit WHERE TYPE = 'pt'", [], function(tx, res)
                                     if(res.rows.length > 0)
                                     console.log("New Port. rows added to tbl_OrgUnit:" + res.rows.item(0).Count);
                                     );
                     );

    
  catch(e)
  
        console.log("Error:" + e.message)
  

配置:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.Vale.Incident" version="1.1.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<preference name="AllowInlineMediaPlayback" value="false" />
<preference name="AutoHideSplashScreen" value="true" />
<preference name="BackupWebStorage" value="cloud" />
<preference name="DisallowOverscroll" value="true" />
<preference name="EnableViewportScale" value="false" />
<preference name="FadeSplashScreen" value="true" />
<preference name="FadeSplashScreenDuration" value=".25" />
<preference name="KeyboardDisplayRequiresUserAction" value="true" />
<preference name="MediaPlaybackRequiresUserAction" value="false" />
<preference name="ShowSplashScreenSpinner" value="true" />
<preference name="SuppressesIncrementalRendering" value="false" />
<preference name="TopActivityIndicator" value="gray" />
<preference name="GapBetweenPages" value="0" />
<preference name="PageLength" value="0" />
<preference name="PaginationBreakingMode" value="page" />
<preference name="PaginationMode" value="unpaginated" />
<preference name="webviewbounce" value="false" />
<feature name="LocalStorage">
    <param name="ios-package" value="CDVLocalStorage" />
</feature>
<name>ReportIncident</name>
<description>
    Hello World sample application that responds to the deviceready event.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
    PhoneGap Team
</author>
<preference name="permissions" value="none" />
<preference name="phonegap-version" value="3.5.0" />
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="false" />
<preference name="ios-statusbarstyle" value="blue" />
<preference name="detect-data-types" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="7" />
<preference name="android-installLocation" value="auto" />
<preference name="AutoHideSplashScreen" value="false" />
<gap:plugin name="org.apache.cordova.battery-status" />
<gap:plugin name="org.apache.cordova.camera" />
<gap:plugin name="org.apache.cordova.media-capture" />
<gap:plugin name="org.apache.cordova.console" />
<gap:plugin name="org.apache.cordova.contacts" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.device-motion" />
<gap:plugin name="org.apache.cordova.device-orientation" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="org.apache.cordova.globalization" />
<gap:plugin name="org.apache.cordova.inappbrowser" />
<gap:plugin name="org.apache.cordova.media" />
<gap:plugin name="org.apache.cordova.network-information" />
<gap:plugin name="org.apache.cordova.splashscreen" />
<gap:plugin name="org.apache.cordova.vibration" />
<!--gap:plugin name="com.millerjames01.sqlite-plugin"-->
<!--plugin name="SQLitePlugin" value="SQLitePlugin" /-->
<icon src="icon.png" />
<icon gap:platform="android" gap:qualifier="ldpi" src="res/icon/android/icon-36-ldpi.png" />
<icon gap:platform="android" gap:qualifier="mdpi" src="res/icon/android/icon-48-mdpi.png" />
<icon gap:platform="android" gap:qualifier="hdpi" src="res/icon/android/icon-72-hdpi.png" />
<icon gap:platform="android" gap:qualifier="xhdpi" src="res/icon/android/icon-96-xhdpi.png" />
<icon gap:platform="blackberry" src="res/icon/blackberry/icon-80.png" />
<icon gap:platform="blackberry" gap:state="hover" src="res/icon/blackberry/icon-80.png" />
<icon gap:platform="ios"  src="res/icon/ios/vale-57.png"  />
<icon gap:platform="ios"  src="res/icon/ios/vale-72.png"  />
<icon gap:platform="ios"  src="res/icon/ios/vale-114.png"  />
<icon gap:platform="ios"  src="res/icon/ios/vale-144.png"  />
<icon gap:platform="webos" src="res/icon/webos/icon-64.png" />
<icon gap:platform="winphone" src="res/icon/windows-phone/icon-48.png" />
<icon gap:platform="winphone" gap:role="background" src="res/icon/windows-phone/icon-173.png" />
<gap:splash gap:platform="android" gap:qualifier="port-ldpi" src="res/screen/android/screen-ldpi-portrait.png" />
<gap:splash gap:platform="android" gap:qualifier="port-mdpi" src="res/screen/android/screen-mdpi-portrait.png" />
<gap:splash gap:platform="android" gap:qualifier="port-hdpi" src="res/screen/android/screen-hdpi-portrait.png" />
<gap:splash gap:platform="android" gap:qualifier="port-xhdpi" src="res/screen/android/screen-xhdpi-portrait.png" />
<gap:splash gap:platform="blackberry" src="res/screen/blackberry/screen-225.png" />
<gap:splash gap:platform="ios"  src="res/screen/ios/splash5-320-480.png"  />
<gap:splash gap:platform="ios"  src="res/screen/ios/splash5-320-480.png"  />
<gap:splash gap:platform="ios"  src="res/screen/ios/splash5-320-480.png"  />
<gap:splash gap:platform="ios"  src="res/screen/ios/splash5-320-480.png"  />
<gap:splash gap:platform="ios"  src="res/screen/ios/splash5-320-480.png"  />
<gap:splash gap:platform="winphone" src="res/screen/windows-phone/screen-portrait.jpg" />
<gap:plugin name="org.apache.cordova.statusbar" version="0.1.4" />
<gap:plugin name="nl.x-services.plugins.videocaptureplus" />
<access origin="*" />
<feature name="Camera">
    <param name="ios-package" value="CDVCamera" />
</feature>
<feature name="Notification">
    <param name="ios-package" value="CDVNotification" />
</feature>
<preference name="CameraUsesGeolocation" value="false" />
<feature name="Vibration">
    <param name="ios-package" value="CDVVibration" />
</feature>
<feature name="File">
    <param name="ios-package" value="CDVFile" />
    <param name="onload" value="true" />
</feature>
<feature name="Capture">
    <param name="ios-package" value="CDVCapture" />
</feature>
<feature name="NetworkStatus">
    <param name="ios-package" value="CDVConnection" />
</feature>
<plugin name="Notification" value="CDVNotification" />
<feature name="FileTransfer">
    <param name="ios-package" value="CDVFileTransfer" />
</feature>
<feature name="File">
    <param name="ios-package" value="CDVFile" />
</feature>
<feature name="InAppBrowser">
    <param name="ios-package" value="CDVInAppBrowser" />
</feature>
<feature name="Globalization">
    <param name="ios-package" value="CDVGlobalization" />
</feature>
<feature name="Media">
    <param name="ios-package" value="CDVSound" />
</feature>
<feature name="Console">
    <param name="ios-package" value="CDVLogger" />
</feature>
<feature name="VideoCapturePlus">
    <param name="ios-package" value="VideoCapturePlus" />
</feature>
<feature name="StatusBar">
    <param name="ios-package" onload="true" value="CDVStatusBar" />
</feature>
<feature name="Device">
    <param name="ios-package" value="CDVDevice" />
</feature>
<feature name="SQLitePlugin">
    <param name="ios-package" value="SQLitePlugin" />
</feature>
<plugin name="SQLitePlugin" value="SQLitePlugin" />
</widget>

问题: 在表格完全加载之前,用户被重定向到登录页面

预期: 只有在数据完全加载后,用户才会被重定向到登录页面。到那时,启动画面应该会显示给用户。

知道怎么做

提前致谢

【问题讨论】:

我们如何才能延迟隐藏屏幕直到数据完全加载或有其他方法可以解决? 你是否已经配置了闪屏插件? 检查我的答案,如果有帮助,请告诉我。 JSON 文件包含超过 10,000 条记录。 是的,在 config.xml 中使用了 【参考方案1】:

在您的config.xml 中添加这一行

<preference name="AutoHideSplashScreen" value="false" />

当你想隐藏splashscree时,在你的js中使用这个函数 navigator.splashscreen.hide();

【讨论】:

没有帮助,仍在导航:( 是的,当闪屏隐藏数据时,数据开始加载到 SQLite 中。 能否请您添加有问题的 config.xml。 使用 Config.xml 更新问题 您在第 4 行使用 &lt;preference name="AutoHideSplashScreen" value="true" /&gt;,请删除它。

以上是关于在angularjs中执行函数后执行代码的主要内容,如果未能解决你的问题,请参考以下文章

使用 AngularJS,如何在视图和控制器同步特定更改后触发代码执行?

AngularJS如何实现异步

请教angularJS 如何 console里直接 执行以下函数

构建自己的AngularJS - 作用域和Digest

AngularJs $interval 和 $timeout

如何在angularjs执行多个异步操作后再执行指定操作