gpt4 book ai didi

javascript - 在同一个 DOM 元素上使用 ng-show 和 ng-hide 是否正确?

转载 作者:可可西里 更新时间:2023-11-01 01:37:41 26 4
gpt4 key购买 nike

我想知道在同一个 DOM 元素上使用 ng-show 和 ng-hide 是否是一个好习惯。

这似乎是一个更好的主意,而不是在单个 ng-show 中使用多个条件,其中一些条件被否定。

告诉我。谢谢!

PS:举个例子

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

最佳答案

绝对不是。

首先,这两个指令可能会相互干扰(请参阅 JSFiddle,由 Joel Skrepnek 提供),这通常只是糟糕的设计。

您可以使用一个函数、另一个字段或只是一些更多的内联逻辑。

内联逻辑:

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

字段:

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

函数

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

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

我的建议是使用另一个字段函数,如果需要检查的值超过 2 个。

关于javascript - 在同一个 DOM 元素上使用 ng-show 和 ng-hide 是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21260161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com