Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Hide button after click

New to Angular and struggling with how to do things the "Angular Way". All I want to do is click a button to show a hidden element inside a view then hide the button that was clicked. Any help would be awesome.

HTML:

<button class="btn btn-primary" ng-click="showDiv=true; hideMe()">
  Show Div
</button>
<div ng-show="showDiv">
  I was hidden now you see me, but how do I hide the button?
</div>

Controller:

  $scope.hideMe = function(){
    console.log('hide the button');
    $scope.hide();
  }

Code sample: Plunker

Ideally would like to incorporate

ng-hide
on the button and not have a function run inside of the controller.
like image 571
wsfuller Avatar asked Dec 09 '22 02:12

wsfuller


2 Answers

<button ng-hide="showDiv"...

Should work also

like image 107
KArneaud Avatar answered Dec 11 '22 09:12

KArneaud


Just add ng-show="!showDiv" to your button, this will hide it if showDiv = true

Just to be clear new html should look:

<button class="btn btn-primary" ng-click="showDiv=true" ng-show="!showDiv">Show Div</button>
like image 34
code Avatar answered Dec 11 '22 09:12

code