【问题标题】:How to conditionally show a button inside ng-repeat in Angular/Meteor如何在 Angular/Meteor 的 ng-repeat 内有条件地显示一个按钮
【发布时间】:2016-04-01 06:55:07
【问题描述】:

我有一个输出数组的 ng-repeat:

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

我希望能够根据另一个集合中是否已有记录有条件地显示按钮。查看一些文档,看起来我应该有一个助手来决定显示按钮,所以我尝试了

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

在我的this.helpers({。但是,这只会输出一次undefined。我的印象是 helper 是响应式的,为什么它只运行一次?

【问题讨论】:

    标签: javascript angularjs meteor


    【解决方案1】:

    this.helpers({}) 仅用于响应式数据源,并且必须通过Collection.find()Collection.findOne()(甚至Meteor.user())返回mongoCursorSee docs

    原语和函数必须在正常范围内声明,但我建议在帮助程序中定义当前用户:

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

    然后在你的按钮中使用ng-show="usr.data.driverId === currentUser._id"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多