角js变量不起作用

时间:2016-02-02 11:15:55

标签: angularjs sqlite cordova ngcordova

的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";

alert消息完美无缺,但当我尝试在taskId语句中使用变量select时,它不起作用。

2 个答案:

答案 0 :(得分:1)

app.controller('Ctrl', function($scope) {
    $scope.removeTask = function(taskId){
    alert("Task Id is "+taskId);
    $scope.taskId = taskId;
};


var statement = "";

 db.transaction(function (tx) {

    //tx.executeSql('DROP TABLE IF EXISTS test_table');

    statement = "select * from objects where o.TYPES='+$scope.taskId+' GROUP BY objectid";

请注意,您必须将任务ID分配给范围变量以便稍后使用。

答案 1 :(得分:0)

您需要在函数内部创建db事务。

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"; 
        };
    };

taskId等函数参数仅在函数内可用。