Ernany Daniel
Ernany Daniel

Reputation: 37

AngularJS ng-show is not working

I'm trying to make the button "Detalhes" to toggle a div to show a message.

Apparently there's nothing wrong. First... my 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:

<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>

Upvotes: 0

Views: 6742

Answers (4)

Brian
Brian

Reputation: 5059

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.

Why is this?

The $scope.visive1 variable does not need to be interpolated through the use of double-brackets in the ng-show directive. In short Directives do not need braces, while expressions DO need them - @Evan Bechtol

Upvotes: 10

Rahul Arora
Rahul Arora

Reputation: 4543

You should do this

ng-show="visivel"

and not

ng-show="{{visivel}}"

Reason why you see 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.

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. Angular knows by itself what you passing to these directives

Upvotes: 1

Swati Maheshwari
Swati Maheshwari

Reputation: 85

Try initializing $scope.visivel = {} in the controller.

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}}"

Upvotes: 0

Alberto
Alberto

Reputation: 878

When you refresh the page you reset the App and therefore you do this:

$scope.visivel = false;

making the div invisible..

Upvotes: 0

Related Questions