ionic: I have a scope variable empty from a stringified json

时间:2015-05-24 20:43:41

标签: json angularjs sqlite filter ionic

I'm trying to filter a JSON result from a SQLite query. The filter works when I use JSON directly, but it doesn't when I use the query from the service. Then, the $scope.arrayme just appears as empty.

Where is the error? Thank you!

This is the service:

getSubtipos: function() {
    var query = "SELECT subtipos.idsubtipo, subtipos.tipos_idtipo, subtipos.nombre, subtipos.icon, subtipos.val FROM subtipos";
    var arraySubtipos = [];
    $cordovaSQLite.execute(db, query, []).then(function(res) {
        if(res.rows.length > 0) {
            for(var i = 0; i < res.rows.length; i++) {
                arraySubtipos.push(res.rows.item(i));
            }
        } else {
            console.log("No results found");
        }
    }, function (err) {
        console.error("ERROR: " + err.message);
    }).finally(function() {
        arraySubtipos =  JSON.stringify(arraySubtipos);
    });
        return arraySubtipos;
    }

This is the controller:

.controller('MenuSubtiposCtrl', function($scope, $filter, miJson, $stateParams, $cordovaSQLite){
    var arrayme = JSON.stringify(miJson.getSubtipos());
    $scope.arrayme = $filter("filter")(JSON.parse(arrayme), {tipos_idtipo: $stateParams.foo});
})

And this is the state:

.state('app.menusubtipos', {
    url: "/menusubtipos/:foo",
    views: {
        'menuContent': {
            templateUrl: "templates/menuSubtipos.html",
            controller: "MenuSubtiposCtrl"
        }
   }
})

1 个答案:

答案 0 :(得分:1)

There may be more problems than what I've immediately noticed, but I have have noticed that you're returning a variable within your getSubtipos function before it's set.

The $cordovaSQL.execute() function is an asyncronous function. As a result, you are returning arraySubtipos before it's set.

A better way to do this would be within getSubtipos to do the following:

var arraySubtipos = [];
return $q.when($cordovaSQLite.execute(db, query, [])
    .then(function(res) {
        if(res.rows.length > 0) {
            for(var i = 0; i < res.rows.length; i++) {
                arraySubtipos.push(res.rows.item(i));
            }
        } else {
            console.log("No results found");
        }
        return JSON.stringify(arraySubtipos);
    }));

// Then, within your controller, do the following:

.controller('MenuSubtiposCtrl', function($scope, $filter, miJson, $stateParams, $cordovaSQLite){
    miJson.getSubtipos()
        .then(function(arrayMe) {
            // No need to stringify it again
            $scope.arrayme = $filter("filter")(JSON.parse(arrayme), {tipos_idtipo: $stateParams.foo});
        })
        .catch(function(error) {
            // Handle the error here
        });
    var arrayme = JSON.stringify(miJson.getSubtipos());
});

I'm also a little suspicious about your use of JSON.stringify and JSON.parse. It's likely that they're not needed, but without knowing the format of your data, I've left that as is.