假设我有以下数组:
(它已经组成,因为我为客户提供的数据无法在此处发布)
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
与当前对象一起使用?
答案 0 :(得分:2)
您可以这样做:
<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
答案 1 :(得分:2)
你可以做这样的事情
控制器
$scope.hasBrowser = function(name) {
return collections.browsers.some(function(browser) {
return browser.label === name;
});
};
HTML
<div ng-show="hasBrowser('Chrome')">content</div>
=)