Angular + ionic $ http.get ng-repeat然后检查$ cordovaSQLite中的数据

时间:2016-07-25 13:53:17

标签: angularjs sqlite ionic-framework

Screen

从$ http中获取图书清单,然后将数据放入ng-repeat 但是,我想检查这本书是否已经在$ cordovaSQLite中,然后显示“go”, 如果这本书不在sqlite中,那么显示“下载”按钮

这是我的代码(如果已经在sqlite中的数据没有显示下载按钮),但它不起作用。

<div class="item item-button-right" ng-repeat="eachPost in posts;">
    {{ eachPost.title }}
    <button class="button button-positive" ng-show="showDownloadBT(eachPost.part)">Download</button>
  </div>

视图是:

my_dict[student_name]

1 个答案:

答案 0 :(得分:0)

这些函数是异步的,所以尝试对函数showDownloadBT进行递归,并在每个帖子showDownload中添加一个属性,如下所示:

var theurl = 'http://server/api/book/getAll';
$http.get(theurl).then(function(res) {
  $scope.posts = res.data.posts;
  var postList = res.data.posts;
  var i = 0;
  showDownloadBT(i)//call first time

  function showDownloadBT (index) {
    var queryDR = "SELECT * FROM BookDownloaded WHERE bookId = ? AND page = ? ";
    $cordovaSQLite.execute(db, queryDR, [book_id, the_page]).then(function(res2) {
      if(res2.rows.length > 0) {
          postList[index].showDownload = false;
      }else{
          postList[index].showDownload = trues;
      }

      //  recursion
      i++;
      if (postList[i]) { //prevents stack overflow
        showDownloadBT(i);
      } else {
        $scope.posts = postList;// end of list
      }
    }, function (err) {
        console.error(err);
    });
  }

并在HTML中:

<div class="item item-button-right" ng-repeat="eachPost in posts;">
    {{ eachPost.title }}
    <button class="button button-positive" ng-show="eachPost.showDownload">Download</button>
  </div>