在Angular JS中重新启用ng-disabled按钮

时间:2015-03-09 06:21:00

标签: javascript jquery html angularjs

我是AngularJS的新手。我创建了一个包含字段的表单,默认情况下使用ng-disabled禁用这些字段。当我点击编辑<button>时,我希望这些字段重新启用。

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>

控制器

  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

      });
  }

4 个答案:

答案 0 :(得分:9)

在控制器创建范围变量

$scope.disabled= true;

并将所有ng-disabled替换为该变量,例如

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

当您点击编辑按钮设置$scope.disabledfalse时,

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

答案 1 :(得分:2)

你可以让一个范围变量保持truefalse值。并为该变量设置一个。

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

}

并且在您的HTML中,您可以绑定该getter函数。

 <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>

答案 2 :(得分:1)

您需要在控制器顶部创建一个变量

$scope.mydisabled=true; 

然后使用变量

设置ng-disable

NG-禁用= “mydisabled”

并单击编辑按钮将其值设置为false

$scope.mydisabled=false;

<强>更新 Fiddle

答案 3 :(得分:1)

另一种(但是类似的)方法是将表单内容包装在字段集中,并且仅在字段集中使用ng-disabled而不是在所有输入字段中。使html更清晰。

<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>

现在在控制器中根据您的逻辑将isFormSetForSaving设置为true / false。

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