[英]AngularJS check condition and return true or false
I have html like this : 我有这样的HTML:
<td><span ng-if="isuser(user) == false" type="button" >not allowed</span> </td>
the above html is displayed only if the function isuser(user)
return false. 仅当函数
isuser(user)
返回false时才显示上述html。
I have an array like this : 我有这样一个数组:
$scope.list = ["456", "111", "459"];
Now the user object has an array called id
. 现在用户对象有一个名为
id
的数组。 This id
array contains a number
key whose value can be 456
, 111
or 459
and sometimes it may be empty
also. 这个
id
数组包含一个number
,其值可以是键456
, 111
或459
和有时也可能是empty
也。
The user object : 用户对象:
{"name":"pravin","status":"Live","id":[{"number":"111"}],"msg":"test"}
Here is the isuser
function : 这是
isuser
函数:
$scope.isuser = function(user) {
for (var i = 0; i < $scope.list.length; i++){
var exists = user.id.find(({number}) => number === $scope.list[i]);
}
if (exists)
return true;
else
return false;
};
but this always returns false. 但这总是返回false。
I want the for loop to check if none of the values of the number key exists in the list array and then only return false. 我希望for循环检查列表数组中是否存在数字键的值,然后只返回false。 How do I do it?
我该怎么做?
The issue is that the below statement 问题在于以下声明
var exists = user.id.find(({number}) => number === $scope.list[i]);
updates every time the exists
variable and always you'll get the last change. 每次
exists
变量时都会更新,并且您将始终获得最后一次更改。
One approach could be using some
method by passing a callback function as argument. 一种方法可以是通过将回调函数作为参数传递来使用
some
方法。
$scope.isuser = function(user) {
var exists = user.id.some(({number}) => $scope.list.includes(number));
return exists;
}
Try this it will help you 试试这个会对你有所帮助
$scope.isuser = function(user) {
var exists = false;
for (var i = 0; i < $scope.list.length; i++){
exists = user.id.find(({number}) => number === $scope.list[i]);
}
if(i==$scope.list.length-1){
if (exists)
return true;
else
return false;
}
loop };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.