简体   繁体   English

为什么ng-show无法根据表达式显示或隐藏我的元素?

[英]Why is ng-show not showing or hiding my element based on the expression?

I have: 我有:

<span class="inlineBlock fa fa-question-circle"
      ng-show="!(validateEmail(ahs.forms.modal.modalUserEmail)=='OK')"
      title="{{ validateEmail(ahs.forms.modal.modalUserEmail) }}">{{ validateEmail(ahs.forms.modal.modalUserEmail) }}</span>

The value of validateEmail is set with the following: validateEmail的值设置如下:

    $scope.validateEmail = function (error) {
        if (error.$error) {
            if (error.$error.required) return "Required";
            if (error.$error.email) return "Email Invalid";
        } 
        return "OK";
    }

But even when it returns OK then the show is still showing the fa-question-circle icon with the word OK next to it. 但是,即使返回OK,该节目仍会在fa-question-circle图标旁边显示OK字样。 Note that I have tried several different combinations of parenthesis and I always see either the words Required, Email Invalid or OK. 请注意,我尝试了几种不同的括号组合,并且总是看到“必需”,“电子邮件无效”或“确定”字样。 I never see the word not showing. 我从来没有看到这个词没有显示。

我唯一的猜测是inlineBlock类将覆盖display CSS属性,在删除该类之后是否还会发生?

Your code works fine "as is" if ahs.forms.modal.modalUserEmail is what it should be, but fails if it is not, and here's the JSFiddle to prove it. 如果ahs.forms.modal.modalUserEmail应该是您的代码,它可以按ahs.forms.modal.modalUserEmail ,但如果不是,则失败,这是JSFiddle进行证明的。

I copy/pasted your code and just added the required objects that you left out from your question. 我复制/粘贴了您的代码,只是添加了您从问题中遗漏的必需对象。

Did you check the console for errors? 您是否在控制台中检查了错误? Angular gets very upset if for example ahs.forms.modal.modalUserEmail should happen to be null . 如果ahs.forms.modal.modalUserEmail应该恰好为null则Angular会非常不高兴。

Now I know what is happening. 现在我知道发生了什么事。 You should pass the input to the validateEmail function in order to check if it contains a valid value. 您应该将输入传递给validateEmail函数,以检查其是否包含有效值。 So, try this: 因此,请尝试以下操作:

<form name="form" >
    <input type="email" name="emailField" ng-model="ahs.forms.modal.modalUserEmail"/>
    <span class="inlineBlock fa fa-question-circle" ng-show="validateEmail(form.emailField) != 'OK'"></span>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM