如何使用ng-repeat angularjs ionic将数据从sqlite显示到列表中

时间:2015-10-02 06:37:02

标签: javascript angularjs sqlite ionic

我正在使用此代码,无法弄清楚如何将数据库中的数据填充到离子列表中

$scope.selectAll = function() {
    $scope.allSessions= [];
    var query = "SELECT * FROM tb_sessions7";
    $cordovaSQLite.execute(db, query, []).then(function(res) {
        if(res.rows.length > 0) {
             console.log("SELECTED -> " + res.rows.item(0).name + " " + res.rows.item(0).amount);
             for (var i=0; i<res.rows.length; i++) {
                $scope.allSessions.push(res.rows.items(i).name);
                //console.log("data->"+$scope.allSessions);
             }
        } else {
            console.log("No results found");
        }
    }, function (err) {
        console.error(err);
    });
}

1 个答案:

答案 0 :(得分:4)

最后我得到了解决方案

$scope.selectAll = function() {

    var query = "SELECT * FROM tb_sessions7";
    $cordovaSQLite.execute(db, query, []).then(function(res) {

        if(res.rows.length > 0) {
             console.log("SELECTED -> " + res.rows.item(0).id + " " + res.rows.item(0).s_id);
             for (var i=0; i<res.rows.length; i++) {

                $scope.allSessions.push({
                    id: res.rows.item(i).id,
                    s_id: res.rows.item(i).s_id,
                    name: res.rows.item(i).name,
                    amount: res.rows.item(i).amount,
                    total_sessions: res.rows.item(i).total_sessions,
                    commission: res.rows.item(i).commission,
                    total_order: res.rows.item(i).total_order,
                    created_date: res.rows.item(i).created_date,
                    last_edit: res.rows.item(i).last_edit
                    });

             }
        } else {
            console.log("No results found");
        }
    }, function (err) {
        console.error("error=>"+err);
    });
}

HTML

<ion-list>
  <ion-item ng-repeat="session in allSessions | filter: searchKey" href="#/app/session/{{session.id}}">
    <h3>{{session.id}} {{session.name}}</h3>
    <p>{{session.amount}}</p>
  </ion-item>
</ion-list>