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>
提案:不要なHTMLを削除し、JavaScriptで構文エラーを修正...(私はあなたには、いくつかのコピー&ペーストを行なったし、いくつかの行が省略されたと思います) –
方法ng-ifで関数を使用しましたか?投稿されたコードには表示されません。 Btw、 '$ scope.check'関数は何も返しません。 – MMhunter
ng-ifで試したコードを1つに変更しても動作しません。 – ewing1990