2016-08-09 7 views
0

私は本当にあなたの助けが必要です。私はすべてが正常に動作していること、機能ng-if = Ionicアプリのcheck(document)機能

画像のパスが存在する場合、返し
$scope.check = function(document) { 
    var url = baseUrl + document.image; 
    $http.get(url).success(function() { 
    return true; 
    }) 
    .error(function() { 
    return false; 
    }); 
}; 

でNGクリックテストボタン

<ion-item ng-repeat="document in filtered = (documents | filter: searchFilter | filter: {category_id: categoryFilter.id}: false | orderBy: sortBy)" type="item-text-wrap"> 
    <div class="document-functions"> 
    <form name="downloadForm" method="post" ng-submit="download(document)"> 
     <button class="buttons button-document-save" ng-if="check(document)">Save</button> 
    </form> 
    </div> 
</ion-item> 

を作成しました。 しかし、この関数をng-ifで試してみたところ、無限ループエラーが返ってきました。理由はわかりません。 イメージパスが存在する場合は、ドキュメントダウンロードボタンを表示しないようにしてください。

+0

提案:不要なHTMLを削除し、JavaScriptで構文エラーを修正...(私はあなたには、いくつかのコピー&ペーストを行なったし、いくつかの行が省略されたと思います) –

+0

方法ng-ifで関数を使用しましたか?投稿されたコードには表示されません。 Btw、 '$ scope.check'関数は何も返しません。 – MMhunter

+0

ng-ifで試したコードを1つに変更しても動作しません。 – ewing1990

答えて

0

ng-ifで$ http webserviceを実行することができないので、ng-ifは$ http応答を待たずに関数をループで高速に実行するため、エラーが発生します。 解決策は、ng-initでwebServiceを1回だけ呼び出し、応答値をドキュメント関連の変数(現在のフィルタリングされた項目)に格納し、ng-ifでこの変数をチェックすることです。

コントローラ

.controller('DocumentCtrl', function($http, $scope, $ionicPopup, $timeout, $cordovaFileTransfer, $cordovaFile, $window, localStorageService) { 
    $scope.checkDownload = function(document) { 
    var url = baseUrl + document.path; 
    var filename = url.split("/").pop(); 
    $cordovaFile.checkFile(cordova.file.documentsDirectory, filename) 
    .then(function(success) { 
     $scope.file[document.id] = true; 
    }, function(error) { 
     $scope.file[document.id] = false; 
    }); 
    }; 
}); 

ビュー

<ion-item ng-repeat="document in filtered = (documents | filter: searchFilter | filter: {category_id: categoryFilter.id}: false | orderBy: sortBy)" type="item-text-wrap" ng-init="checkDownload(document)"> 
    <form class="view-form" name="viewForm" method="post" ng-submit="openLink(document, user)" ng-if="file[document.id] == false"> 
    <button type="submit"><img class="document-image" image-lazy-src="{{::url + document.thumbnail }}" alt="" image-lazy-loader="ios"/></button> 
    </form> 
    <form class="view-form" name="viewForm" method="post" ng-submit="openPath(document, user)" ng-if="file[document.id] == true"> 
    <button type="submit"><img class="document-image" image-lazy-src="{{::url + document.thumbnail }}" alt="" image-lazy-loader="ios"/></button> 
    </form> 
    <div class="document-title" ng-if="document.title.length <= 25">{{::document.title}} <span class="document-size">({{::document.size | filesize:2}})</span></div> 
    <div class="document-title" ng-if="document.title.length > 25">{{::document.title | limitTo: 25}} ...<span class="document-size"> ({{::document.size | filesize:2}})</span></div> 
    <div class="document-functions"> 
    <form name="downloadForm" method="post" ng-submit="download(document)"> 
     <button class="buttons button-document-save" ng-if="file[document.id] == false">Save</button> 
    </form> 
    <form name="sendForm" method="post" ng-submit="send(document, user, data)"> 
     <button class="buttons button-document-send">Email file</button> 
    </form> 
    </div> 
</ion-item> 
関連する問題