Angularjs 使用WEB_SQL的脱机应用程序

Angularjs 使用WEB_SQL的脱机应用程序,angularjs,web-sql,Angularjs,Web Sql,我正在使用WEB-SQL和angularJS开发脱机应用程序 我面临着很多问题,我无法通过适当的解决方案来解决 当应用程序启动时,我如何知道我需要创建数据库,因为应用程序第一次启动或浏览器已重置。 为了解决这个问题,我正在做什么 'use strict'; angular.module('Services') .service('LocalDb', function($q){ var db = openDatabase('LocalDb', '1.0', 'Local database', 10

我正在使用WEB-SQL和angularJS开发脱机应用程序

我面临着很多问题,我无法通过适当的解决方案来解决 当应用程序启动时,我如何知道我需要创建数据库,因为应用程序第一次启动或浏览器已重置。 为了解决这个问题,我正在做什么

'use strict';
angular.module('Services')
.service('LocalDb', function($q){
var db = openDatabase('LocalDb', '1.0', 'Local database', 100 * 1024 * 1024);
return {
    getData : function(condition){
        var defer = $q.defer();
        if(condition == null)
            var query = "select * from TABLENAME";
        else
            var query = "select * from TABLENAME where COLUMNNAME ='" + condition + "'";
        db.transaction(function(tx){
            tx.executeSql(query, [], success, failure);
        });
        function success(tx, results){
            defer.resolve(results.rows.item(0));
        };
        function failure(tx, err){
            defer.reject(err.message);
        }
        return defer.promise;
    }
  }
});
下面是我的控制器,它在应用程序引导时运行,我在其中获取数据,如果找不到数据,则调用API从服务器加载数据: 我知道我没有正确处理数据库,因为我找不到管理web sql API的最佳实践。这是我的第一个脱机项目

try{
    var Template = LocalDb.getData('a71391ea11e5f4e2bb1ec0cecdec5c69').then(function(response){
        if(response){
            scope.baseObject = JSON.parse(response.baseTemplateObject);
        }
        else{
            callApi = true;
        }
    });    
}
catch(err){
    console.log(err.message);
    callApi = true;
}
if(callApi) {
    /*calling api and creating data base then creating table and then inserting response data into table */ 
}
问题是应用程序启动时并没有表,所以控制器中的函数甚至不会执行

此外,我也无法找到如何处理像这样的回调

  • 找不到表错误
  • 找不到数据错误
  • 找不到数据库错误
  • websql的异步调用也会产生一些问题
有谁能帮我做这件事的最佳实践吗

任何帮助都是巨大的支持。 提前谢谢