[英]Disable input with dynamically generated fields in Angular
Imagine this: 想象一下:
http://jsfiddle.net/wcuuj8do/9/ http://jsfiddle.net/wcuuj8do/9/
My current code: 我当前的代码:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.rowData = [];
$scope.addRow = function(title, number)
{
$scope.rowData.push({
'title': title,
'number': number
});
};
$scope.addRow('Car', '1200');
$scope.addRow('Car','');
}
When i type "Car" inside first input (T1) and then type some text to input (N1) i want angular to check each T# input if has same value as (T1). 当我在第一个输入(T1)内键入“汽车”,然后输入一些文本以输入(N1)时,我想按角度检查每个T#输入是否具有与(T1)相同的值。 If has disable (or readonly) all N# inputs related to currently checked T# input. 如果已禁用(或只读),则与当前检查的T#输入相关的所有N#输入。
Afterwards when i remove duplicated values from T# fields, related T# fields should be returned to default input states (remove disable / readonly) 之后,当我从T#字段中删除重复的值时,相关的T#字段应返回到默认输入状态(删除禁用/只读)
This should work by adding new dynamic inputs as seen in fiddle. 如小提琴所示,这应该通过添加新的动态输入来起作用。
You should create a method, that will do the checking part. 您应该创建一个方法,该方法将进行检查。 This method will should be bound to blur
or change
event on T#
inputs, depends on what you want. 此方法应绑定到T#
输入上的blur
或change
事件,具体取决于您想要的内容。
The method will check for duplicity, and if found, mark the object, eg add new property disabled: true
. 该方法将检查是否重复,如果找到,则标记该对象,例如,添加新属性disabled: true
。 This property will be then used in the template on N#
fields via ng-disabled
directive. 然后,将通过ng-disabled
指令在N#
字段的模板中使用此属性。
Here is your update fiddle: http://jsfiddle.net/wcuuj8do/10/ 这是您的更新小提琴: http : //jsfiddle.net/wcuuj8do/10/
Note the new method $scope.checkDuplicity
and new binding: 注意新方法$scope.checkDuplicity
和新绑定:
<tr ng-repeat="(key, item) in rowData">
<td>T{{ ($index+1) }}: <input type="text" ng-model="rowData[key].title" ng-change="checkDuplicity(key)" value="" style='margin-bottom:15px' /></td>
<td>N{{ ($index+1) }}: <input type="text" ng-model="rowData[key].number" value="" style='margin-bottom:15px' ng-disabled="rowData[key].disabled" /></td>
</tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.