正确使用NG-节目和NG隐藏相同的DOM元素? [英] Is correct to use ng-show and ng-hide on the same DOM element?

查看:152
本文介绍了正确使用NG-节目和NG隐藏相同的DOM元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果是用NG-节目和NG隐藏相同的DOM元素一个很好的做法。

I was wondering if is a good practice to use ng-show and ng-hide on the same DOM element.

这似乎是一个更好的了解,而不是使用多个条件,其中的一些被否定,在一个单一的NG-显示

It seems a better idea, instead of using multiple conditions, some of which negated, in a single ng-show.

让我知道。
谢谢!

Let me know. Thanks!

PS:这里的例子

< D​​IV NG秀=isBlondeNG隐藏=hasBlueEye> Mary是金发碧眼,她有绿色的眼睛< / DIV>

推荐答案

绝对不是。

首先,这两个指令可以在彼此跳闸(请参阅本 的jsfiddle ,作为提供通过 乔尔Skrepnek ),一般只是糟糕的设计。

First of all, the two directives can trip over each other( see this JSFiddle, as provided by Joel Skrepnek), and is generally just bad design.

您可以使用一个函数,另一场或者只是一些更多的嵌入式逻辑。

You can use a function, another field or just some more inline-logic.

直列逻辑:

Inline-Logic:

<div ng-show="isBlonde && !hasBlueEye">Mary is blonde and she has green eyes</div>

字段:

Field:

<div ng-show="shouldShowThisDiv">Mary is blonde and she has green eyes</div>

功能

Function

<div ng-show="shouldShowThisDiv()">Mary is blonde and she has green eyes</div>

$scope.shouldShowThisDiv = function(){
    return $scope.isBlonde && !$scope.hasBlueEye;
}

我的建议会为使用另一个字段功能后,如果有需要检查超过2的值。

My recommendation would be to use either another field or a function, if there are more than 2 values that needs to be checked.

这篇关于正确使用NG-节目和NG隐藏相同的DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆