【发布时间】:2017-11-02 07:06:32
【问题描述】:
我的功能有什么问题?函数 getCart 无法显示存储在 WebSQL 中的数据。但是我的函数 addToCart 可以工作并且可以将数据存储到 WebSQL。如果您知道我的错误,请帮助我。
angular.module('login').factory('CartService',
['$webSql', '$http', '$q', 'urls',
function ($webSql, $http, $q, urls) {
var factory = {
initdb: initdb,
addToCart: addToCart,
getCart: getCart
};
var db = null;
return factory
function initdb(){
db = openDatabase('CART', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function(transaction){
transaction.executeSql("CREATE TABLE IF NOT EXISTS cart (id INTEGER PRIMARY KEY, name TEXT, price DOUBLE)");
});
}
function addToCart(name, price){
var item = {
"name" : name,
"price" : price
}
console.log('Success add cart');
db.transaction(function(transaction) {
transaction.executeSql("INSERT INTO cart (name, price) VALUES (?, ?)", [name, price]);
getCart();
});
}
function getCart(name){
var item = {
"name" : name
}
db.transaction(function(transaction) {
transaction.executeSql("SELECT * FROM cart name = ?", [name]);
});
}
}
]);
【问题讨论】:
-
你需要为executeSql实现回调处理程序才能看到从websql db获取的记录。
-
在我的 getCart 函数中先生?
-
是的,我刚刚添加了答案。
标签: javascript angularjs web-sql