wsfuller
wsfuller

Reputation: 1840

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.

Upvotes: 4

Views: 24419

Answers (2)

Kendall
Kendall

Reputation: 5255

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

Should work also

Upvotes: 8

code
code

Reputation: 1137

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>

Upvotes: 6

Related Questions