warhead3030
warhead3030

Reputation: 11

AngularJs + Sqlite - Populate variable in sqlite callback function

i have a problem to populate a variable in a sqlite callback function. So how can i make this var populated? Because i want to return the value from the query.

Here is my code.

Situation 1:

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: im using onsen UI.

Upvotes: 1

Views: 1163

Answers (1)

KNaito
KNaito

Reputation: 3962

To obtain the query results, you should substitute the response object to $scope object and you should execute the $scope.$apply method because the transaction callback is out of AngularJS managing.

For example, the executeSql callback is

                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');
                    }
                );

Upvotes: 2

Related Questions