gpt4 book ai didi

javascript - Angular JS 在 ng-repeat 中设置点击对象的值

转载 作者:行者123 更新时间:2023-11-28 18:33:18 26 4
gpt4 key购买 nike

我有一个 ng-repeat ,它迭代对象,例如:

{
"id": 1,
"title": "Lorem Ipsum",
"base_text": "Lorem ispum",
"read_more_text": "lorem ipsum",
"show_details": false,
"button_label": "Read More",
"button_link": "",
"image": ""
},

在 html 中的 ng-repeat 内,有一个按钮,我想切换该 show_details 属性的值。

所以,我有一个 ng-click 处理程序:

<div class="hero-item" ng-repeat="h in dash.heroItems">
<div>
<h3>{{h.title}}</h3>
<p>{{h.base_text}}</p>
<p ng-if="h.show_details">{{h.read_more_text}}</p>
</div>
<div>
<button ng-click="dash.showHeroDetails(h.id)">{{h.button_label}}</button>
</div>
</div>

所以我传递了重复实例的 ID 属性。但在这里我陷入了困境,我无法获得切换该 bool 属性的正确语法。

到目前为止我有这个:

  _this.showHeroDetails = function (item) {
_this.show_details = item;
};

这显然是错误的......

最佳答案

您只需更新 ng-click 中的值即可。像这样:

<button ng-click="h.show_details = !h.show_details">{{h.button_label}}</button>

关于javascript - Angular JS 在 ng-repeat 中设置点击对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576334/

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