【问题标题】:Allow both upper case and lower case inside ng-if在 ng-if 中允许大写和小写
【发布时间】:2018-01-08 04:42:01
【问题描述】:

我有这样的情况

<table   ng-if="name=='john' && name=='MARK'">
//rest of the code
</table>

我的代码将在 ng-if 为真时执行。

现在我的疑问是,即使名称是大写和小写,ng-if 也应该为真..

<table   ng-if="name=='JOhN' && name=='maRK'">
    //rest of the code
    </table>

有可能吗? 我也尝试过使用if-else 条件。

if(name.toString()='john'){
alert('success')
}
else{
alert('failure')
}

对吗?

【问题讨论】:

    标签: javascript jquery angularjs


    【解决方案1】:

    试试,

    <table   ng-if="name.toLowerCase()=='john' && name.toLowerCase()=='mark'">
    //rest of the code
    </table>
    

    或使用角度 lowercase 过滤器

    <table   ng-if="(name| lowercase) =='john' && (name | lowercase)=='mark'">
       //rest of the code
    </table>
    

    【讨论】:

      【解决方案2】:

      我知道这已经得到回答和接受,但我会用一个函数来做,然后返回检查给定名称的结果和一个接受的名称数组。这样 - 以后更容易更改名称或根据需要添加其他名称。

      //html
      <table   ng-if="checkName(name)">
         //rest of the code
      </table>
      
      
      //js 
      checkName(name){
        let acceptedNames = ['john','mark'];
        let nameStr = name.lowerCase();
        let result = false;
        if(acceptedNames.indexOf(nameStr) > -1 ){result = true};
        return result;
      }
      

      【讨论】:

        【解决方案3】:

        更好的方法是使用过滤器| uppercase)将两个值转换为相同的大小写并进行比较

        【讨论】:

          猜你喜欢
          • 2021-08-31
          • 2020-12-22
          • 1970-01-01
          • 2014-02-08
          • 1970-01-01
          • 1970-01-01
          • 2020-03-30
          • 1970-01-01
          • 2012-11-13
          相关资源
          最近更新 更多