gpt4 book ai didi

javascript - 在 AngularJs 中从数据库中获取并显示数据

转载 作者:行者123 更新时间:2023-11-29 19:29:34 24 4
gpt4 key购买 nike

如何在屏幕上弹出我的客户列表,该列表来 self 的数据库,我有 11 行客户,我想创建一个 ListView 并显示它们,这是我的代码:

dbFactory.method = function findAll() {
db.transaction(
function(tx) {
var sql = "SELECT (nome) as nomes FROM clientes";
log(sql);
tx.executeSql(sql, [],
function(tx, results) {
var len = results.rows.length,
clientes = [],
i = 0;
for (; i < len; i = i + 1) {
clientes[i] = results.rows.item(i).nomes;
}
log(clientes + ' found');


}
);
},txErrorHandler,
function () {

}
);

};

Controller :

 .controller('ListaCtrl', function ($scope, dbFactory) {


$scope.items = dbFactory.method();
console.log(dbFactory.method());


});

客户端.html:

<ion-view view-title="Playlists" ng-app="starter">
<ion-content>
<ion-list>
<ion-item ng-repeat="itens in items" >

<p>{{itens.nome}}</p>

</ion-item>
</ion-list>
</ion-content>
</ion-view>

日志:

 Promise {$$state: Object, then: function, catch: function, finally: function}
$$state: Object
status: 1
value: SQLResultSet
insertId: [Exception: DOMException: Failed to read the 'insertId' property from 'SQLResultSet': The query didn't result in any rows being added.]
rows: SQLResultSetRowList
length: 11
__proto__: SQLResultSetRowList
rowsAffected: 0

最佳答案

我已经设法使用 deferred 和 promise 解决了这个问题,这是我所做的:

var getMessages = function findAll() {
db.transaction(
function(tx) {
var sql = "SELECT (nome) as nomes FROM clientes";
tx.executeSql(sql, [],
function(tx, results) {
var len = results.rows.length,
clientes = [],
i = 0;
for (; i < len; i = i + 1) {
clientes[i] = results.rows.item(i).nomes;
}
log(clientes + ' rowsss found');
deferred.resolve(clientes);

}
);
},txErrorHandler,
function () { }
);
return deferred.promise;

};

return {
showClientes: getMessages
};

Controller :

.controller('ListaCtrl', function ($scope, dbFactory) {

dbFactory.showClientes().then(function(listview) {
$scope.clientes = listview;
console.log($scope.clientes);
});

});

和 html:

<ion-view view-title="Playlists" ng-app="starter">
<ion-content>
<ion-list>
<ion-item ng-repeat="cliente in clientes">

{{cliente}}

</ion-item>
</ion-list>

</ion-content>
</ion-view>

现在我可以看到我所有客户的 ListView 。

关于javascript - 在 AngularJs 中从数据库中获取并显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28719565/

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