Ng-if="check(document)"

Hello guys!

I really need your help, this is more angular specified trouble. I have created save button, which display depends on ng-if statement.

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

Check function.

$scope.check = function(document) {
  var url = baseUrl + document.image;
  $http.get(url).success(function() {
    return true;
  })
  .error(function() {
    return false;
  });
};

It returns infinite loop error, I have no idea why.
The idea is not to show document download button, if image path exists.

1 Like

What is the error??
> Error: cordova is not defined
Remember that cordova plugins not works in browser.

I know about cordova, I changed code a bit, so it’s closer to my problem.
The error goes in loop.

could you attach a screen of console to see error ?

You are returning true/false to the promise method, thats never gonna work. You should check into promises ( $q, available in angular) and resolve/reject after the check. But im not quite sure wether that would work in the ng-if construction.

Buttt, if i read your code correctly, there’s a possibility the image doesn’t exist? When you do it this way, you’ll have the user download a lot files…

you cannot run a $http webservice in ng-if.
because ng-if execute the function in loop so fast without waiting for the$http response and that what cause the errors.
I think the solution would be to call the webService just once in ng-init, store the respond value in a document related variable (the current item of filtered) , and then check this variable in ng-if.

Thanks, @haytam!
Got it working with your advice.

1 Like