2016-07-25 6 views
0

Screen

ブックリストは$をhttpからグラブであるが、その後、しかしNGリピート にデータを入れて、私がしたいかどうかを確認この本がすでに$ cordovaSQLiteに入っているかどうか確認してください。 もしsqliteに本がない場合は、 "ダウンロード"ボタンを表示してください。

ここは私のコードです(既にsqliteのデータがあれば、ダウンロードボタンが表示されます)、動作しません。

var theurl = 'http://server/api/book/getAll'; 
$http.get(theurl).then(function(res) { 
    $scope.posts = res.data.posts; 

    $scope.showDownloadBT = function(part){ 
    var result = true; 
    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) { 
      result = false; 
     }else{ 
      result = true; 
     } 
    }, function (err) { 
     console.error(err); 
    }); 
    return result; 
    } 

図である。

<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> 

答えて

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> 
+0

ng-repeatで "getBooksResponse"と呼ぶにはどうすればいいですか? – Ryan

+0

これは一例ですが、コールバックまたはpromisseことができます...あなたは現在機能 –

+0

が落ち着いて '$ http'を呼び出し、この関数を呼び出すことができ、私は私がそれを得た今 –

関連する問題