2014-09-28 50 views
1

我有一个问题来填充sqlite回调函数中的变量。那么我怎样才能让这个var填充?因为我想从查询中返回值。AngularJs + Sqlite - 在sqlite回调函数中填充变量

这是我的代码。

情况1:

module.controller('MasterController7', function($scope, $data) { 
 
    var hihi = []; 
 
    $scope.queryResult = function() { 
 

 
\t \t database = window.openDatabase("Apps", "1.0", "Apps Database", 200000); 
 
\t \t database.transaction(function(tx){ 
 
\t \t var sql = ""; 
 
\t \t \t \t \t 
 
\t \t tx.executeSql(sql, [], 
 
\t \t  function(tx, response) 
 
\t \t  { \t 
 
       //hihi is not populated, the query ran successfuly. 
 
\t \t \t  hihi.push({title:'title',label:'label',desc:'desc'}); 
 
\t \t  }, 
 
\t \t  function(err) 
 
\t \t  { 
 
\t \t \t  //alert('aaaaaaa'); 
 
\t \t  } 
 
     ); 
 
     }, errorDB, successDB); 
 
\t \t \t \t 
 
    }; 
 
\t \t \t 
 
\t $scope.queryResult(); 
 
\t $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:使用温泉UI即时通讯。

回答

2

要获得查询结果,你应该替代响应对象$范围对象 ,你应该执行$范围。因为事务回调超出 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'); 
        } 
       );