我有一个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
属性。但是在这里我被困住了,我无法获得用于切换该布尔属性的正确语法。到目前为止,我有:
_this.showHeroDetails = function (item) {
_this.show_details = item;
};
这显然是错误的...
最佳答案
从模板传递对象。
<div>
<button ng-click="dash.showHeroDetails(h)">{{h.button_label}}</button>
</div>
在脚本中,只需切换值即可。
_this.showHeroDetails = function (item) {
item.show_details = !item.show_details;
};