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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28719565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:26:31  来源:igfitidea点击:

Fetch and display data from database in AngularJs

javascriptangularjscordovaphonegap-pluginsionic-framework

提问by Stack two

How can I pop the list of my clients on the screen, the list is coming from my database, I have 11 rows of clients and I want to create a listview of those and show them, this is my code:

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

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());


    });

clientes.html:

客户.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>

The log:

日志:

 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

采纳答案by Stack two

I've managed to solve that using deferred and promise, here is what I did:

我已经设法使用 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
        };

The Controller:

控制器:

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

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

    });

And the html:

和 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>

Now I can see my listview with all my clients.

现在我可以看到我所有客户的列表视图。

回答by Ravi Babu Karikati

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


        dbFactory.method().$promise.then(
                 function(res){
                       $scope.items = res; //or res.data
                 },
                 function(){
                      //error call back
                 }
         );

    });