最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - re enabling ng-disabled button in Angular JS - Stack Overflow

programmeradmin6浏览0评论

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 = ".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

      });
  }

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

      });
  }
Share Improve this question edited May 11, 2015 at 15:17 scniro 17k8 gold badges66 silver badges107 bronze badges asked Mar 9, 2015 at 6:21 ShaSha 1,9746 gold badges35 silver badges60 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

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;
});

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>

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

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;
      });
  }
发布评论

评论列表(0)

  1. 暂无评论