且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ng-show和ng-hide有什么区别?

更新时间:2021-07-05 21:44:34

在我之前从事的项目中,我发现同时选择ng-showng-hide很有用.原因是因为我的导航栏中有一个链接,该链接仅应显示用户是否在特定视图上.这是这种情况:

On a project I was working on before, I found having the option of both ng-show and ng-hide useful. The reason being is because I had a link in my navbar that was only supposed to show if the user was on a specific view. Here is that scenario:

<li ng-hide="isActive('/about') || isActive('/contact')" ng-class="{ 'vert-nav-active': isActive('/investigator')}" class="top-buffer">
<a href="#/investigator" class="buff-sides navListLinks">Investigator Portal</a>
</li>

现在,您可能会说,好吧,您可以使isActive('/about') || isActive('/contact')返回相反的布尔值并将ng-hide更改为ng-show,所有内容都将保持不变,但是正如您所看到的,我也正在使用它函数确定我在哪个链接上.如果我颠倒了这个逻辑,那么看起来好像我在每个链接上,但我所连接的实际链接除外.允许我可以ng-show编写另一个函数,但是我喜欢重用已经存在的代码.

Now, you might say, well you could just make the isActive('/about') || isActive('/contact') return the opposite Boolean and change the ng-hide to ng-show and every thing will stay the same but as you can see I'm also using this function to determine which link I'm on. If I reverse this logic, it will look like I'm on every link except the actual link I'm on. Granted I could write another function for the ng-show but I like reusing code that's already there.