javascript - 如何在 Angular/Meteor 中有条件地显示 ng-repeat 内的按钮

标签 javascript angularjs meteor

我有一个 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 一次。我的印象是助手是 react 性的,那么为什么它只运行一次?

最佳答案

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

基元和函数必须在正常范围内声明,但我建议在助手中定义当前用户:

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

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

关于javascript - 如何在 Angular/Meteor 中有条件地显示 ng-repeat 内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618427/

相关文章:

javascript - 使用 Javascript Regex 转义 Lucene 字符

javascript - 账号onLogin hook meteor 循环

javascript - 如何获取模板内的span文本内容

angularjs - 禁止在查询参数更改时重新加载基于 ui-router 的 View

javascript - 蒙戈错误: cursor killed or timed out - Meteor timeout settings ineffective

使用innerHTML的Javascript无法在HEAD中添加脚本标签

javascript - onClick 由变量填充而不触发它

javascript - 将回调函数与原型(prototype)函数一起使用

javascript - 定义模块时依赖注入(inject)不起作用

javascript - 将新属性添加到另一个具有不同长度的数组的对象数组中,因此一旦数组完成迭代,它将再次从第一个开始