0

I'm new to angularjs and wanted to know how to hide a button after clicking (using ng-click) on it.

<button ng-click="xyz()" class="btn-default pull-right">
    Start
</button>
7
  • <button ng-click="xyz()" class="btn-default pull-right">Start</button> Commented Jun 26, 2018 at 9:38
  • You want to hide own button or another one Commented Jun 26, 2018 at 9:39
  • I want to hide the same Start button Commented Jun 26, 2018 at 9:40
  • Hide or disable? Commented Jun 26, 2018 at 9:40
  • It shouldn't be visible, hence prefer to hide it Commented Jun 26, 2018 at 9:42

4 Answers 4

3

Basically, you needs two things:

  • A variable leveraging the button visibility
  • A function to update this variable (you could do it in the HTML but I discourage it).

So you would have your button:

<button ng-click="hideButton()" ng-show="isButtonVisible === true" class="btn-default pull-right">
    Start
</button>

Then, you would have the following variables

$scope.isButtonVisible = true; // true to make the button visible by default

And finally, the function that toggles it:

$scope.hideButton = function() {
    $scope.isButtonVisible = false;
}

Note that you could use ng-if to remove the button from the DOM if you won't need it again.

Example: https://plnkr.co/edit/fnW8HR58zKHs4T34XRan

Note that this is pretty much the most basic question you could have on AngularJS, so I would advice you to read a bit about it before asking Stack Overflow.

Sign up to request clarification or add additional context in comments.

Comments

0

You will need a variable to denote the visibility of the button, its value will change with click event.

<button ng-click="clickEventFunction(params)" ng-hide="isButtonVissible">Button</button>

The default value of this variable should be "false" to show the button

$scope.isButtonVissible = false

Then in the clickEventFunction, change the value to hide the button

$scope.clickEventFunction = function(params){
$scope.isButtonVissible = true; 
//* Do the logic code
}

2 Comments

isButtonVissible is a very confusing name for a variable that hides a button when it is true.
Not a technical issue, but the spelling should be visible, not vissible
-1

I found the answer to the question with some assistance. Created an event in the click function and was able to hide the button.

<button ng-click="xyz($event)" class="btn-default pull-right">Start</button>

$scope.xyz = function ($event) {
    $($event.target).hide();

Cheers to all your guidance.

2 Comments

That's pretty much the worse you could come up with. This is totally against angular principles. Here, you're using jQuery to hide an element where you should do it with Angular. Read my answer to see the correct way on doing this.
Okay I will check on that as suggested by you.
-1

In View:

 <button ng-click="hideBtn = true" ng-hide="hideBtn">Button</button>

In controller :

 $scope.hideBtn = false;

1 Comment

isButtonVissible is a very confusing name for a variable that hides a button when it is true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.