我在sqlite回调函数中填充变量时遇到问题。那么如何使该变量填充?因为我想从查询中返回值。
这是我的代码。
情况一:
module.controller('MasterController7', function($scope, $data) {
var hihi = [];
$scope.queryResult = function() {
database = window.openDatabase("Apps", "1.0", "Apps Database", 200000);
database.transaction(function(tx){
var sql = "";
tx.executeSql(sql, [],
function(tx, response)
{
//hihi is not populated, the query ran successfuly.
hihi.push({title:'title',label:'label',desc:'desc'});
},
function(err)
{
//alert('aaaaaaa');
}
);
}, errorDB, successDB);
};
$scope.queryResult();
$scope.items = hihi;
});
<ons-list ng-controller="MasterController7">
<ons-list-item modifier="chevron" class="item" ng-repeat="item in items" ng-click="menu.setMainPage('favoritesurah.html', {closeMenu: true}); showDetail($index, item.title)">
<ons-row>
<ons-col width="60px">
<div class="item-thum"></div>
</ons-col>
<ons-col>
<header>
<span class="item-title">{{item.title}}</span>
<span class="item-label">{{item.label}}</span>
</header>
<p class="item-desc">{{item.desc}}</p>
</ons-col>
</ons-row>
</ons-list-item>
</ons-list>
p / s:即时通讯使用Onsen UI。
最佳答案
要获取查询结果,应将响应对象替换为$ scope对象
并且您应该执行$ scope。$ apply方法,因为事务回调已超出
AngularJS管理。
例如,executeSql回调是
tx.executeSql(sql, [],
function(tx, response)
{
for (var i=0; i<response.rows.length; i++ ) {
var row = response.rows.item(i);
hihi.push({title:row.id,label:row.label,desc:row.name});
$scope.items = hihi;
$scope.$apply();
}
},
function(err)
{
//alert('aaaaaaa');
}
);
关于angularjs - AngularJs + Sqlite-在sqlite回调函数中填充变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26088741/