gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:00:15 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com