简体   繁体   English

使字段只读取角度

[英]Make fields readonly in angular

I am making a form where user can add more fields by clicking on add more button. 我正在创建一个表单,用户可以通过单击添加更多按钮添加更多字段。 for that I am using ng-repeat and when user click on add more button one field is pushed to array in ng-repeat results into one more field. 因为我正在使用ng-repeat,当用户点击添加更多按钮时,一个字段被推送到ng-repeat结果中的数组到另一个字段。

Now for some cases ng-repeat array may include some fields , I want to make them readonly but if user click on add more button then that field can be editable. 现在,对于某些情况,ng-repeat数组可能包含一些字段,我想让它们只读,但如果用户点击添加更多按钮,那么该字段可以编辑。 My code : 我的代码:

HTML code HTML代码

 <div ng-repeat="field in ui_fields">
     <label for="Language">Field Name :</label><input class="form-control" type="text" ng-model="field.name">
     <label for="Language">Field type :</label>
     <select class="form-control" ng-model="field.type">
         <option value="">Select field type</option>
         <option value="timedate">Date & Time</option>
         <option value="text">Text</option>
     </select>
 </div>

Angular code 角度代码

$scope.add_extra_field = function(){
    $scope.ui_fields.push({ 
        name: "",
        type: ""
      });
    }

Use an extra field isreadonly in ui_fields array and the ngReadOnly directive, like : ui_fields数组和ngReadOnly指令中使用额外字段isreadonly ,如:

Your HTML: 你的HTML:

<div ng-repeat="field in ui_fields">
    <label for="Language">Field Name :</label><input class="form-control" ng-readonly="field.isreadonly" type="text" ng-model="field.name">
    <label for="Language">Field type :</label>
    <select class="form-control"  ng-disabled="field.isreadonly" ng-model="field.type">
        <option value="">Select field type</option>
        <option value="timedate">Date & Time</option>
        <option value="text">Text</option>
    </select>
</div>

Your javascript: 你的javascript:

$scope.add_extra_field = function(){
    $scope.ui_fields.push({ 
        name: "",
        type: "",
        isreadonly: false
      });
    }

I'm not sure about your exact use case, but you can use ngReadonly to make controls conditionally read only. 我不确定您的确切用例,但您可以使用ngReadonlyngReadonly地将控件设置为只读。 In this example I made the last row readonly: 在这个例子中,我只读了最后一行:

http://plnkr.co/edit/sZhKsjWoFBh30ikKZeN8?p=preview http://plnkr.co/edit/sZhKsjWoFBh30ikKZeN8?p=preview

<input class="form-control" type="text" 
       ng-model="field.name" ng-readonly="$index < ui_fields.length - 1" />

Edit: I forked to match your actual use case http://plnkr.co/edit/DT7oMAhkjGxGa1GRN5uP?p=preview 编辑:我分叉以匹配您的实际用例http://plnkr.co/edit/DT7oMAhkjGxGa1GRN5uP?p=preview

In the save function you use to send the edited data to the server you can set the index of last saved data. 在用于将编辑数据发送到服务器的保存功能中,您可以设置上次保存数据的索引。 Use this index as condition for ngReadonly 使用此索引作为ngReadonly条件

<input class="form-control" type="text" 
       ng-model="field.name" ng-readonly="$index <= savedIndex" />

Controller: 控制器:

$scope.add_extra_field = function(){
  $scope.ui_fields.push({ 
      name: "",
      type: ""
    });
  }
$scope.save = function() {
   // send to server
   $scope.savedIndex = $scope.ui_fields.length -1;
}

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

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