如何在数组中存在具有特定值的键时使用ng-show
How to use ng-show when a key with a certain value exists in an array?
假设我有以下数组:
(这是编造的,因为我为一个客户工作,他的数据我不能在这里发布)
collections: {
browsers: [
{
label: 'Chrome',
visitors: 50,
bounces: 23
},
{
label: 'FireFox',
visitors: 30,
bounces: 15
},
{
label: 'Internet Explorer',
visitors: 60,
bounces: 33
},
{
label: 'Opera',
visitors: 4,
bounces: 2
},
]
}
如果我想在我的模板中显示Chrome的数据,目前它是可用的,我通常会使用这样的东西:
<div ng-show="collections.browser.chrome">content</div>
但是因为它是一个数组,所以这不起作用
是否有一种方法来使用ng-show
与当前对象?
你可以这样做:
<div ng-repeat="browser in collections.browsers">
<div ng-show="browser.label == currentBrowser">content</div>
</div>
这个HTML将基于控制器上的这些值
$scope.collections = //Your JSON Here
$scope.currentBrowser = 'chrome'; //Or whatever you want
控制器你可以这样做
$scope.hasBrowser = function(name) {
return collections.browsers.some(function(browser) {
return browser.label === name;
});
};
html <div ng-show="hasBrowser('Chrome')">content</div>
=)
相关文章:
- AngularJS-ng使用字符串索引在数组上重复
- angularjs ng基于点击时的数组值重复过滤器
- ng将数组重复解析为字符串
- Angularjs:ng重复对象数组
- 使用ng-src增强对象内部的数组
- 替换 ng-repeat 数组会导致奇怪的重新渲染
- 如何在 meanJS 中存储 ng-repeat 数组表单数据
- ng在数组中重复数组
- Angularjs ng使用数组中相同的元素重复动画
- * ng用于数组的动态字符串插值
- 在web服务调出后使用ng-show显示数组长度
- Ng-show基于数组中存在的值
- 滚动到ng-repeat数组中最后添加的记录
- 绑定ng-options数组中的一个属性
- 不能从AngularJS数据中NG-Repeat数组(英雄联盟)
- 动态添加项目到ng-repeat数组javascript
- 如何在数组中存在具有特定值的键时使用ng-show
- AngularJS的ng-repeat数组的数组
- Data-ng-show和数组元素
- 如何让AngularJS更新DOM上的ng-repeat数组拼接?$scope.$apply()返回一个错误