【问题标题】:Getting sqlite data to scope in angular in hybrid mobile app在混合移动应用程序中以角度范围获取 sqlite 数据
【发布时间】:2016-07-15 08:24:53
【问题描述】:

您好,我正在尝试使用 sqlite 创建一个移动应用程序进行存储。我为 sqlite db 创建了一个服务,并尝试将数据从数据库获取到控制器范围,我尝试使用回调,但不起作用。请帮忙。

这是sqlite服务中的代码

  self.getAll = function(table_name, cb) {

    this.db.transaction(function(transaction) {
        transaction.executeSql("SELECT * FROM " + table_name, [],
            function(tx, result) {


                cb(result.rows);
            },
            function(error) {
                console.log("Error occurred while getting the data.");
                console.log(error);
            });
    });
}

这是主控制器中的代码

 function getData(data) {
 console.log("logging from getData");
 console.log(data);

 $scope.all = data;

}

sqlite.getAll("todo", getData);


console.log('logging results outside');
console.log($scope.all);

结果没有分配给 $scope.all 变量,我可以做些什么来获得结果,getData 函数中的控制台日志可以工作并显示表数据。

【问题讨论】:

  • 我正在尝试在不使用服务器的情况下构建独立的移动应用程序。想用手机中的sqlite存储来保存数据
  • 那么抱歉评论,我要删除它
  • @fodma1 如果他使用的是 sqllite,它会在前端运行。 Bmax,你能不能发一个plunkr
  • @Bmax 最后一个 console.log($scope.all) 将始终未定义,因为它在数据到达之前运行。而是在您的 html 中显示所有变量,例如 {{ all }}
  • @johan 谢谢它的工作,但必须使用 $timeout

标签: javascript angularjs sqlite callback hybrid-mobile-app


【解决方案1】:

最后一个 console.log($scope.all) 将始终未定义,因为它在数据到达之前运行。而是在您的 html 中显示 all 变量,例如 {{ all }} 或者在变量上使用 $watch,或者在数据到达后在函数内部进行赋值。看你想做什么。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多