javascript - 无法更新 View ,数据绑定(bind)不起作用 - Angularjs 和 PhoneGap

标签 javascript android cordova angularjs

我正在尝试使用 PhoneGap(适用于 Android 设备)制作一个简单的待办事项应用程序。我还使用 AngularJS 进行数据绑定(bind)。
我想显示保存在数据库中的任务列表。当我使用 chrome 调试器进行调试时,我可以看到 SQL 请求有效,但当我在模拟器或设备上启动应用程序时没有任何显示。

DbCtrl.js

var myApp = angular.module('myApp', []);

function DbCtrl($scope) {
$scope.init = function() {
    document.addEventListener("deviceready", onDeviceReady, false);     
}

function onDeviceReady() {
     var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
     db.transaction(populateDB, errorDB, successDB);
 }

 function populateDB(tx) {
    tx.executeSql('DROP TABLE IF EXISTS DEMO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, todo)');
    tx.executeSql('INSERT INTO DEMO (id, todo) VALUES (1, "first todo")');
    tx.executeSql('INSERT INTO DEMO (id, todo) VALUES (2, "Second todo")');
 }

 function errorDB(err){
    alert("Error processing SQL: "+err)
 }

 function successDB(){
    var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
    db.transaction(queryDB, errorDB);
 }

 // Query the database
function queryDB(tx) {
    tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorDB);
}

// Query the success callback
function querySuccess(tx, results) {
    console.log("Returned rows = " + results.rows.length);
       $scope.todos = results.rows;
}

$scope.init();
}

DbIndex.html

<body ng-app="myApp" >

    <div ng-controller="DbCtrl">    

        <div>
            <h1>SimpleTodos</h1>
        </div>

        <div id="mainContent">
            <ul ng-repeat="todo in todos">
               <li>{{todo.id}}</li>
            </ul>
        </div>      
        <div class="ui-bar">
            <a href="edit.html">Add Note</a>
        </div>

    </div>


    <script src="../libs/cordova-2.5.0.js"></script>
    <script src="../libs/angular-1.0.5.js" type="text/javascript" ></script>
    <script src="dbCtrl.js" type="text/javascript" ></script>
</body>

有人知道为什么 View 没有更新吗?我们有什么办法吗?

最佳答案

这是因为查询是异步的。更新模型后,您必须触发摘要,以便 Angular 可以更新 View 。

// Query the success callback
function querySuccess(tx, results) {
    console.log("Returned rows = " + results.rows.length);
    $scope.todos = results.rows;
    $scope.$apply(); //trigger digest
}

关于javascript - 无法更新 View ,数据绑定(bind)不起作用 - Angularjs 和 PhoneGap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16087970/

相关文章:

javascript - 不变违规 : findComponentRoot when using Link & Tables

javascript - 向 Angular 2 中的子组件发送搜索输入值

android - PhoneGap 和 WhatsApp

javascript - cordova.file 在 android 的 ionic 项目中未定义

javascript - 将多个元素放入 JavaScript 数组

javascript - 类型错误: module.PosModel 未定义

java - ''Launch MainActivity' ' 代码阻止文本显示

android - 搜索在 RecyclerView 部分不起作用

android ndk-gdb 无法加载符号

javascript - Phone Gap 是否能够将 php web 应用程序转换为 iphone/android 应用程序?