javascript - 单击一个元素的子元素时如何隐藏所有其他元素

标签 javascript angularjs

我需要能够通过单击同样由 ng-repeat 创建的其他元素来隐藏由 ng-repeat 动态生成的元素。 HTML 示例:

<div ng-repeat="thing in things" ng-show="!displayId || displayId == 'thing.id'">
    <button ng-click="displayId = 'thing.id'">Click me!</button>
</div>

初始显示工作正常,因为在单击按钮之前 displayId 未指定,但我期望发生的情况是将 displayId 设置为 事物。我点击的任何按钮的 id ,一旦设置,所有其他 div 都会被隐藏。然而,什么也没有发生。

有办法实现这一点吗?我试图不使用任何 jquery 并以纯粹的 Angular 方式执行此操作。

最佳答案

因此,根据您提供的额外详细信息,您可以在 ng-repeat 上使用 filter

<div ng-repeat="thing in things | filter:{id: selectedThingId}">
    <button ng-click="setSelectedThing(thing.id)">Click me!</button>
</div>

在你的 Controller 中:

$scope.selectedThingId;
$scope.setSelectedThingId = function setSelectedThingIdFn(thingId) {
  $scope.selectedThingId = thingId;
}

这是一个嵌入式 Plunker 示例:

<iframe style="width: 100%; height: 600px" src="http://embed.plnkr.co/tpMSLcpEGkoWx4eGDcHo/preview" frameborder="0" allowfullscren="allowfullscren"></iframe>

关于javascript - 单击一个元素的子元素时如何隐藏所有其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701089/

相关文章:

Javascript对象相关

javascript - 将字符串数组与另一个字符串数组进行比较的优雅方法

javascript - 使用Jquery拖放,从拖放的div中获取值

javascript - 获取站点加载脚本和库的进度

javascript - 如何在 AngularJS 中实现服务器端搜索框

javascript - 无法从 javascript 书中理解这些示例,好的部分

javascript - 如何对 Polyfilled WebComponents 自定义元素进行单元测试

javascript - AngularJS 中变量的文本输入

javascript - 过滤搜索 AngularJS

javascript - AngularJS:双向数据绑定(bind)在 Controller 中找不到模型