简体   繁体   English

AngularJS ng-show不起作用

[英]AngularJS ng-show is not working

I'm trying to make the button "Detalhes" to toggle a div to show a message. 我试图使按钮“详细信息”切换div以显示消息。

Apparently there's nothing wrong. 显然没有错。 First... my HTML 首先...我的HTML

            <tr ng-repeat="chamado in cabertos">
                            <td>{{chamado.numero}}</td>
                            <td>{{chamado.user}}</td>
                            <td>{{chamado.assunto}}</td>
                            <td>{{chamado.status_chamado}}</td>
                            <td><button ng-click="mostra()">Detalhes</button></td>
                        </tr>                               
                    </tbody>
                    <div ng-show="{{visivel}}">
                        <h3>Mensagem enviada:</h3>
                            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                            proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                    </div>
                 </table>
            </div>

My Script: 我的剧本:

app.controller('mostra',function($scope){       
    $scope.visivel = false;
    $scope.mostra = function() {
        if($scope.visivel==false) $scope.visivel=true;
        else if($scope.visivel == true) $scope.visivel=false;
    };
});

And the when I press F12 in my page, for an unknown reason there is a ng-hide not allowing me to toggle my div: 当我在页面中按F12时,由于未知原因,有一个ng-hide隐藏,不允许我切换div:

<div ng-show="false" class="ng-hide">
                        <h3>Mensagem enviada:</h3>
                            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                            proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                    </div>

Change 更改

<div ng-show="{{visivel}}">

To

<div ng-show="visivel">

Edit - adding explanation why this is the case. 编辑-添加解释为什么是这种情况。 I am quoting Evan, as I could not explain this any better than he. 我引用埃文(Evan),因为我无法比他更好地解释这一点。

Why is this? 为什么是这样?

The $scope.visive1 variable does not need to be interpolated through the use of double-brackets in the ng-show directive. 不需要在ng-show指令中使用双括号对$ scope.visive1变量进行插值。 In short Directives do not need braces, while expressions DO need them - @Evan Bechtol 简而言之,指令不需要大括号,而表达式则需要大括号 -@Evan Bechtol

You should do this 你应该做这个

ng-show="visivel"

and not 并不是

ng-show="{{visivel}}"

Reason why you see class="ng-hide" 您看到class =“ ng-hide”的原因

since your ng-show is false, angular applies class ng-hide which hides the element as it is opposite of show, if ng-show was true it would have removed the class. 由于您的ng-show是假的,所以angular应用了ng-hide类,该类隐藏了与show相反的元素,如果ng-show是true,它将删除该类。

Also you do not need to use the curly braces (interpolation) along with pre defined angular JS directives like ng-show, ng-hide, ng-if, ng-repeat. 同样,您也不需要将花括号(插值)与预定义的角度JS指令(例如ng-show,ng-hide,ng-if,ng-repeat)一起使用。 Angular knows by itself what you passing to these directives Angular本身知道您要传递给这些指令的内容

When you refresh the page you reset the App and therefore you do this: 刷新页面时,将重置应用程序,因此您可以执行以下操作:

$scope.visivel = false;

making the div invisible.. 使div不可见。

Try initializing $scope.visivel = {} in the controller. 尝试在控制器中初始化$scope.visivel = {}

app.controller('mostra',function($scope){    
 $scope.visivel = {};   
    $scope.visivel = false;
    $scope.mostra = function() {
        if($scope.visivel==false) $scope.visivel=true;
        else if($scope.visivel == true) $scope.visivel=false;
    };
});

Also in the HTML use ng-show="visivel" instead of ng-show="{{visivel}}" 同样在HTML中使用ng-show="visivel"代替ng-show="{{visivel}}"

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

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