how to use if in a function in knockout js?

后端 未结 3 1401
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 05:42

i have a condition in my function . i want to set a value of a variable true or false on the basis of another variable whether it is empty of not in knockout js?



        
3条回答
  •  离开以前
    2021-01-29 06:29

    You have used two different variables (nsct and nsc) and an operator that doesn't exist (=!). The last part of the condition would be interpreted as an assignment: nsc = (!"").

    Also, the logic is wrong, there is no value that is null and an empty string at the same time, so the condition would always be true. You would use the && operator instead:

    if (nsct != null && nsct != "") {
      self.view(true);
    }
    

    If you want to set it to false if the condition isn't true, then you would use an else also:

    if (nsct != null && nsct != "") {
      self.view(true);
    } else {
      self.view(false);
    }
    

    You can also do that by using the value of the condition:

    self.view(nsct != null && nsct != "");
    

提交回复
热议问题