【发布时间】:2014-01-29 15:54:30
【问题描述】:
当用户在文本字段中输入的文本通过验证时,我想生成一个新的文本框。现在当新文本框的文本有效时,应该生成另一个文本框。
问题是我做不到,因为创建的名称属于“email1”“email2”“email3”类型。而且我无法访问这些动态生成的字段。
JS 片段:
var master = {
name: 'John Smith',
address: {
line1: '123 Main St.',
city: 'Anytown',
state: 'AA',
zip: '12345'
},
contacts: [{
type: 'phone',
value: 'This is a value',
validationtype: 'number'
}]
};
$scope.addContact = function(contact) {
var valueOfContact = $scope.form.contacts.value;
console.log(valueOfContact);
if ($scope.myForm.email.$valid == true) {
tbCounter++;
$scope.form.contacts.push({
type: 'email',
value: '',
name: 'email' + tbCounter,
id: 'email' + tbCounter
});
console.log($scope.form.contacts.indexOf(contact), $scope.form.contacts.length);
}
};
在 If 子句中我希望能够访问 email1、email2 等等。
HTML 片段:
<div ng-repeat="contact in form.contacts">
<label>{{ contact.type }}</label>
<input name ="{{ contact.name }}" type="{{ contact.type }}" ng-model="contact.value" ng-change=addContact(contact) required/>
<span class="error" ng-show="myForm.email.$error.required">required</span>
<span class="error" ng-show="myForm.email.$error.email">invalid email</span>
</div>
谢谢。
【问题讨论】:
-
你能提供工作的提琴手吗?
标签: javascript angularjs validation input