Search code examples
angularjssqlitecordovangcordova

angular js variable not work


index.html

<div class="test" ng-controller="Ctrl">
    <button ng-click="removeTask(10);">remove</button>
<div>

app.js

app.controller('Ctrl', function($scope) {
    $scope.removeTask = function(taskId) {
        alert("Task Id is " + taskId);
    };
    var statement = "";
    db.transaction(function (tx) {
        //tx.executeSql('DROP TABLE IF EXISTS test_table');
        statement = "select * from objects where o.TYPES=' + taskId' GROUP BY objectid";

The alert message work perfectly, but when I try to use the variable taskId on select statement it does not work.


Solution

  • You need to make the db transaction inside the function.

    app.controller('Ctrl', function($scope) {
        var statement = "";
        $scope.removeTask = function(taskId) {
            alert("Task Id is " + taskId);
    
            db.transaction(function (tx) {
            //tx.executeSql('DROP TABLE IF EXISTS test_table');
            statement = "select * from objects where o.TYPES='+taskId' GROUP BY objectid"; 
            };
        };
    

    Function arguments such as taskId are available only inside the function.