简体   繁体   English

ng-show不会隐藏按钮

[英]ng-show does not hide a button

I have a selector with more options, I want to hide a button if the first option of the selector is selected. 我有一个包含更多选项的选择器,如果选择了选择器的第一个选项,我想隐藏一个按钮。

<select ng-model="$ctrl.type">
    <option value="">none</option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>

as it can be seen, the first option has value ="" . 可以看出,第一个选项有value =""

This is the button: 这是按钮:

    <div ng-show="$ctrl.type!=""" class="btn action-btn" ng-click="$ctrl.doSomething()">
        my button
    </div>

inside the controller I have this code: 在控制器内我有这个代码:

class MyCtrl {
  constructor(...) {
    ...
  }

  doSomething() {
    this.type = "";
  }
}

Why doesn't it work? 为什么不起作用?

它应该是,

<div ng-show="ctrl.type!=''" class="btn action-btn" ng-click="$ctrl.doSomething()">

你可以试试这个:

<div ng-show="!ctrl.type" class="btn action-btn" ng-click="$ctrl.doSomething()">

Your ng-show condition is incorrect as it is not checking the empty value. 您的ng-show条件不正确,因为它没有检查空值。 To make it work you need to change your code to, 要使其工作,您需要将代码更改为,

<div ng-show="ctrl.type!=''" class="btn action-btn" ng- 
 click="$ctrl.doSomething()">
    my button
</div>

You can further enhance this to use trim() to get rid of whitespaces from ctrl.type and get proper result during comparison. 您可以进一步增强此功能以使用trim()来消除ctrl.type的空格并在比较期间获得正确的结果。 For this you can use, 为此您可以使用,

<div ng-show="ctrl.type.trim()!=''" class="btn action-btn" ng- 
 click="$ctrl.doSomething()">
    my button
</div>

the main issue is that you are using the same double quotes when you should be using different ones. 主要问题是当你使用不同的双引号时,你使用相同的双引号。

<div ng-show="ctrl.type !== ''" class="btn action-btn" ng-click="$ctrl.doSomething()">
        my button
</div>

This being said I would use a Boolean flag for this, because it makes the code and the checks much easier to read and understand: 这就是说我会为此使用布尔标志,因为它使代码和检查更容易阅读和理解:

doSomething() {
    if ( type !== '' )
    this.showForType = true;
  }


<div ng-show="ctrl.showForType === true" class="btn action-btn" ng-click="$ctrl.doSomething()">
        my button
</div>

you don't necessarily need a check for true, but it does help from a maintenance and readability point of view. 您不一定需要检查true,但从维护和可读性的角度来看它确实有帮助。

The same could be done with something like: 同样可以通过以下方式完成:

<div ng-show="ctrl.showForType" class="btn action-btn" ng-click="$ctrl.doSomething()">
        my button
</div>

If any part of your angular controlled UI elements show while the right states are not set you can use ng-cloak to stop that from happening : https://docs.angularjs.org/api/ng/directive/ngCloak 如果角度控制的UI元素的任何部分显示而未设置正确的状态,则可以使用ng-cloak来阻止这种情况发生: https//docs.angularjs.org/api/ng/directive/ngCloak

You can do it by these three ways 你可以通过这三种方式来做到这一点

one

ng-hide="$ctrl.type==''" // correct way and related naming functionality ng-hide="$ctrl.type==''" //正确的方式和相关的命名功能

two

ng-show ="$ctrl.type!=''" // correct way but unrelated naming for the functionality ng-show ="$ctrl.type!=''" //正确的方式但功能的无关命名

three

ng-if="$ctrl.type!=''" // correct way without render the DOM elements ng-if="$ctrl.type!=''" //正确的方法而不渲染DOM元素


Kindly check : what is the difference between ng-if and ng-show/ng-hide 请检查: ng-if和ng-show / ng-hide之间的区别是什么

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

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