简体   繁体   English

AngularJS:如何禁用表单中的所有表单控件?

[英]AngularJS:how to disable all the form controls in a form?

I want to disable all the form components so that it can't be edited when view button is clicked. 我想禁用所有表单组件,以便在单击视图按钮时无法编辑它。 this is my form 这是我的表格

<form action="#" class="form-horizontal" >
   <div class="form-group">
      <label for="fieldname" class="col-md-3 control-label">Name</label>
      <div class="col-md-6">
         <input type="text" ng-model="newItem.customSelected" typeahead="name as name.name for name in members | filter:{name:$viewValue}" class="form-control" />
      </div>
   </div>
   <div class="form-group">
      <label for="fieldhname" class="col-md-3 control-label">House name</label>
      <div class="col-md-6">
         <input type="text" ng-model="newItem.customSelected1" typeahead="house_name as house_name.house_name for house_name in family | filter:{house_name:$viewValue}" class="form-control" />
      </div>
   </div>
   <div class="form-group">
      <label for="" class="col-md-3 control-label"><?php echo $this->lang->line('label_family_id'); ?></label>
      <div class="col-md-6">
         <input type="text" ng-model="newItem.customSelected2" typeahead="fm as fm.family_reg_no for fm in family | filter:{family_reg_no:$viewValue}" class="form-control" />
      </div>
   </div>
   <div class="col-md-3"></div>

</form>

and this is my button 这是我的按钮

<input type="button" class="finish btn-success btn" ng-click="view(newItem)" value="view"/>

您可以将所有表单元素放入fieldset并使用ng-disabled来禁用整个字段集,而不是在每个字段级别处理它。

you can use fieldset tag by surrounding your buttons with a fieldset and using ng-disabled attribute: 您可以使用fieldset通过与周围的按钮标签fieldset ,并使用ng-disabled属性:

<form action="#" class="form-horizontal" >
   <fieldset ng-disabled="isClicked">
      <!--your form here --!>
   </fieldset>
</form>

now all that left is in the view(newItem) function do: 现在剩下的就是在view(newItem)函数中执行:

$scope.view = function(newItem){
   $scope.isClicked = true;
   // Your code here and set it to false when your are done with it
}

您可以使用叠加并在其上显示ng-show,或者您可以添加到每个输入ng禁用

  1. Set a scope variable when the view button is clicked. 单击视图按钮时设置范围变量。
  2. Use ng-disabled in combination with the scope variable to disable the fields. ng-disabled与范围变量结合使用可禁用字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM