简体   繁体   English

AngularJS检查条件并返回true或false

[英]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 ,其值可以是键456111459和有时也可能是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.

相关问题 在AngularJs中,使用ng-if时更好的方法是在模板中执行条件或调用返回true或false的函数 - In AngularJs which way is better while using ng-if, is to perform the condition in the template or call a function that return true or false 如何检查禁用按钮为真/假? 在angularjs中 - How to check the disabled button is true/false ? in angularjs 从laravel返回true或false到Angularjs $ scope - Return true or false from laravel to Angularjs $scope Firebase 检查节点是否存在返回 true 或 false - Firebase check if node exist return true or false 可以使用函数在do…while中检查条件是真还是假。 - It's possible to use a function to check if the condition is true or false in do…while? 最好的方法也是检查 React Js 中的 false & true 条件 - Best way too check the false & true condition in React Js 返回true或返回false - Return true or Return false Javascript - 如果满足条件,则在特定时间内返回 True,然后返回 false - Javascript - If condition is met, return True for specific amount of time, then false 如何编写在AngularJS中返回true / false的服务? - How do I write a service that return true/false in AngularJS? 为什么这个函数只返回真,因为没有 else 语句使它在非真条件下返回假? - Why does this function return only true, since there is no else statement to make it return false under a non true condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM