angularjs - 如何在angularjs中显示特定的div onclick
问题描述
我有几个用于 onclick 的 div 和几个 div 来显示它一次 onclick,但我只想显示与特定 div 相关的特定 div。这里我的应用程序工作正常,但是对于 2 个不同的部分它重复,如果我第一次单击第四个 div也显示。我也给出了不同的功能。任何人都可以帮助我,我是 angularjs 的新手。下面是代码。
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
<script src="app.js"></script>
<body ng-app="plunker" ng-controller="MainCtrl">
<div ng-repeat="item in items">
<div style="color:red" ng-click="onClick1($index,0)">first</div>
<div style="color:blue" ng-show="div_[$index+'_0']">yyy</div>
<br />
<hr />
</div>
<div ng-repeat="item in items2">
<div style="color:red" ng-click="test($index,0)">second</div>
<p style="color:blue" ng-show="div_[$index+'_0']">zzz</p>
<br />
<hr />
</div>
</body>
应用程序.js
angular.module('plunker', [])
.controller('MainCtrl', function($scope) {
$scope.div_=[];
$scope.items = [
{
// id: 1,
title: "first item"
},
{
// id: 2,
title: "second item",
},
{
// id: 3,
title: "third item",
}
];
$scope.items2 = [
{
id: 5,
title: "first item 1"
},
{
id: 6,
title: "second item 2",
},
{
id: 7,
title: "third item 3",
}
];
$scope.onClick1 = function (indexa, row1){
alert('first');
$scope.div_[indexa+'_'+row1]=true;
}
$scope.test = function (indexb, row2){
$scope.div_[indexb+'_'+row2]=true;
}
});
解决方案
您正在使用相同的变量来显示/隐藏 div,您已经构建了一些东西来解决这个问题,但没有使用。尝试这个:
<div style="color:red" ng-click="test($index, 1)">second</div>
<p style="color:blue" ng-show="div_[$index+'_1']">zzz</p>
现在div_[$index+'_0']
是div_[$index+'_1']
并且test($index, 0)
必须是test($index, 1)
推荐阅读
- javascript - 在 JavaScript 中将对象转换为另一种类型的对象
- docker - Harbor 复制规则限制为 10 个最新图像
- linux - 如何使用自定义内核启动 ubuntu 云映像
- typescript - 使用 eslint 和 prettier 在 typescript 中配置换行符
- javascript - 如何在js中查找数组是否包含随机数[Math.floor((Math.random() * 10) + 1)]
- android - 在不损失图标质量的情况下压缩应用程序中的图标
- c# - 将 CommandLineParser 与选项模式相结合
- sum - 如果日期在月份,Apple 数字总和
- reactjs - 在 React 中将字符串时间转换为 12 小时格式
- laravel - laravel 访问器不能使用 select() 语句