gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:27 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com