Sha
Sha

Reputation: 1974

re enabling ng-disabled button in Angular JS

I am a newbie to AngularJS. I have created a form with fields which is disabled using ng-disabled by default. when I click on the edit <button> I want these fields to re-enable.

HTML

  <form class="form-horizontal" role="form" ng-submit="edit_setting()" ng-controller="ExchangeController">
    <div class="form-group">
      <label>Name</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" ng-model="exchange_dt.name" ng-disabled="true">
      </div>
    </div>
    <div class="form-group">
      <label>Host Name</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" ng-model="exchange_dt.host_name" required ng-disabled="true">
      </div>
    </div>
    <div class="form-group">
      <label>Address</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" ng-model="exchange_dt.address" ng-disabled="true">
      </div>
    </div>
  </form>

Controller

  function ExchangeController($scope, $http, $cookieStore, $location) {
    var edit_exchange_setting = "https://pvbp.com/api/settings.html?contactid=292351&exchange_id=7124&clearinghouseid=1&token=e5349652507c0esae86d50fdbdc53222cf97&page=view";
      $http.get(edit_exchange_setting).success(function(response){
        $scope.exchange_dt.exchange_name = response.name;
        $scope.exchange_dt.exchange_host_name = response.host_name;
        $scope.exchange_dt.exchange_last_processed_date = response.address;   
      });

      $scope.edit_exchange_setting_click = (function(){
      // how can i make the fields re enable here

      });
  }

Upvotes: 8

Views: 30252

Answers (4)

V31
V31

Reputation: 7666

A different (however similar) approach is to wrap your form contents in a fieldset and have ng-disabled in the fieldset only rather than in all the input fields. To make the html more cleaner.

<form class="form-horizontal" role="form" ng-submit="edit_setting()" ng-controller="ExchangeController">
  <fieldset ng-disabled ="isFormSetForSaving">
    <div class="form-group">
      <label>Name</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" ng-model="exchange_dt.name">
      </div>
    </div>
    <div class="form-group">
      <label>Host Name</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" ng-model="exchange_dt.host_name" required>
      </div>
    </div>
    <div class="form-group">
      <label>Address</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" ng-model="exchange_dt.address">
      </div>
    </div>
   </fieldset>
  </form>

and now in the controller set the isFormSetForSaving to true/false as per your logic.

function ExchangeController($scope, $http, $cookieStore, $location) {
    $scope.isFormSetForSaving = true;
    var edit_exchange_setting = "https://pvbp.com/api/settings.html?contactid=292351&exchange_id=7124&clearinghouseid=1&token=e5349652507c0esae86d50fdbdc53222cf97&page=view";
      $http.get(edit_exchange_setting).success(function(response){
        $scope.exchange_dt.exchange_name = response.name;
        $scope.exchange_dt.exchange_host_name = response.host_name;
        $scope.exchange_dt.exchange_last_processed_date = response.address;   
      });

      $scope.edit_exchange_setting_click = (function(){
      // how can i make the fields re enable here
          $scope.isFormSetForSaving = false;
      });
  }

Upvotes: 1

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

in controller create scope variable,

$scope.disabled= true;

and replace all ng-disabled with that variable like,

...ng-model="exchange_dt.name" ng-disabled="disabled"....

when you click on edit button set $scope.disabled to false like,

$scope.edit_exchange_setting_click = (function(){      
    $scope.disabled = false;
});

Upvotes: 9

Divya MV
Divya MV

Reputation: 2053

you can have a scope variable keeping the true or false value.and a setter for that variable.

  function ExchangeController($scope, $http, $cookieStore, $location) {
var edit_exchange_setting = "https://pvbp.com/api/settings.html?contactid=292351&exchange_id=7124&clearinghouseid=1&token=e5349652507c0esae86d50fdbdc53222cf97&page=view";
  $http.get(edit_exchange_setting).success(function(response){
    $scope.exchange_dt.exchange_name = response.name;
    $scope.exchange_dt.exchange_host_name = response.host_name;
    $scope.exchange_dt.exchange_last_processed_date = response.address;   
  });

  $scope.edit_exchange_setting_click = (function(){
  // how can i make the fields re enable here

  });

  $scope.dtName=true;
   $scope.isdtNameDisabled=function()
    {
      return $scope.dtName;
    };
  $scope.updatedtName=function(flag)
  {
  $scope.dtName=flag;
};

}

and in your HTML you can bind that getter function.

 <div class="form-group">
  <label>Name</label>
  <div class="col-sm-6">
    <input type="text" class="form-control" ng-model="exchange_dt.name" ng-disabled="isdtNameDisabled()>
  </div>
</div>

Upvotes: 2

squiroid
squiroid

Reputation: 14017

You need to create a variable at the top of controller say

$scope.mydisabled=true; 

then set your ng-disable with the variable

ng-disabled="mydisabled"

and on click of edit button set its value to false

$scope.mydisabled=false;

UPDATE Fiddle

Upvotes: 1

Related Questions