Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question

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');
                    }
                );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.