简体   繁体   中英

How to conditionally show a button inside ng-repeat in Angular/Meteor

I have an ng-repeat outputting an array:

<div ng-repeat="usr in myPage.data.people track by $index">
  <button ng-click="leaveReviewUsr($index)" ng-show="isReviewable(usr)">
      review
  </button>
</div>

I would like to be able to conditionally show the button, based on if there is already a record in another collection. Looking at some documentation it looks like I should have a helper to decide to show the button, so I tried

isReviewable(usr) {
  console.log(usr);
  if(this.data) {
    console.log(this.data.driverId + " / " + Meteor.userId());
    if(this.data.driverId == Meteor.userId()) {
      console.log(usr);
      return true;
    }
  }
  return false;
}

in my this.helpers({ . However this just outputs undefined once. I was under the impression that the helper is reactive, so why is it only running once?

this.helpers({}) is for reactive datasources only and must return a mongoCursor via Collection.find() or Collection.findOne() (or even Meteor.user() ). See docs .

Primitives and functions must be declared in the normal scope, but I propose instead defining the current user in a helper:

this.helpers({
    currentUser() { return Meteor.user(); }
});

and then using ng-show="usr.data.driverId === currentUser._id" in your button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM