javascript 基于数组中存在的值的 ng-show
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24525295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ng-show based on a value present in an array
提问by Govan
How to show a vlaue in angularjs ng-show like
如何在 angularjs ng-show 中显示一个 vlaue
ng-show = "role in ['admin', 'user', 'buyer']"
need to show a div if role is any of the things in that array.
如果角色是该数组中的任何东西,则需要显示一个 div。
回答by guru
Use the function indexOf
in array to find whether a element is part of the array. It will return the position of the element if found or will return -1.
使用indexOf
数组中的函数来查找元素是否是数组的一部分。如果找到,它将返回元素的位置,否则将返回 -1。
So you can use an expression like
所以你可以使用这样的表达式
ng-show = "['admin', 'user', 'buyer'].indexOf(role)!=-1"
to show/hide data
ng-show = "['admin', 'user', 'buyer'].indexOf(role)!=-1"
显示/隐藏数据
回答by gorpacrate
Or using loDash _.contains.
或者使用 loDash _.contains。
It needs loDash to be accessible in scope.
它需要 loDash 才能在范围内访问。
var app = angular.module('myApp', []);
app.run(function($rootScope){
$rootScope._ = _;
});
ng-show = "_.contains(['admin', 'user', 'buyer'], role)"