4

I have used 2 button in the form. I want to disable button1 on initialisation though I have given ng-disabled="true" but whenever user clicks on button2, button1 get enabled. Can anyone tell me how to do this in angularjs ?

1
  • Show us your code Commented Mar 2, 2017 at 9:11

4 Answers 4

4

You don't need to do anything in the controller if you are not working with the scope variable inside the controller.

So just do something like:

angular.module("app",[]).controller("ctrl",function($scope){})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
        <div ng-app="app" ng-controller="ctrl">
          <button ng-disabled="first !== true"> one</button>
          <button ng-click="first = true"> two</button>
        </div>

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

Comments

2

You can call one function on click of second button and set the ng-disabled value to false.

DEMO

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
  $scope.inactive= true;
  $scope.enableButton1 = function() {
    $scope.inactive= false; 
  }      
});
<div ng-app="myApp" ng-controller="MyCtrl">
  <br><input type="button" ng-disabled="inactive" value="button1"/>
  <br><input type="button" ng-click="enableButton1()"  value="button2"/>
</div>

Comments

1

use scope variables and assign them to ng-disabled

angular.module("app",[])
.controller("ctrl",function($scope){
	$scope.firstBtn = true;
	$scope.secondBtn = false;
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <button ng-disabled="firstBtn"   > one</button>
  <button ng-disabled="secondBtn" ng-click="firstBtn = false"> two</button>
</div>

Comments

0

Its very simple as given below:

JS

var app = angular.module('myApp', []);
app.controller('ctrl', function($scope) {
  $scope.isDisabled = true;
  $scope.toggleButton = function() {
    $scope.isDisabled = !$scope.isDisabled;
  }
});

HTML

<div ng-app='myApp'>
  <div ng-controller='ctrl'>
    <button ng-click='toggleButton()'>
      Toggle
    </button>
    <button ng-disabled='isDisabled'>
      I am button
    </button>
  </div>
</div>

Here is the jsfiddle link Jsfiddle demo

Comments

Your Answer

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