我试图在我的模板驱动表单中以angular 7形式对kendo-dropdownlist进行必需的字段验证。如果您看到我正在循环并生成一个动态表,该表的每一行都有kendo dropdownlist。如果未选中下拉菜单,则需要突出显示。我试图用div标签将div括起来,以为当用户按下Submit时我可以处理它,但是我认为这更多是在kendo中进行设置。有人可以告诉我这样做的方法。到目前为止,无论看到什么示例,都基于jquery。
这是堆叠闪电战 https://stackblitz.com/edit/angular-4v2k8f
HTML
form name="form" (ngSubmit)="f.form.valid && createDocument()" #f="ngForm" novalidate>
<div class="center" class="file-upload-wrapper">
<ngx-file-drop dropZoneLabel="Drop files here" dropZoneClassName="file-drop" multiple="true"
(onFileDrop)="dropped($event)" (onFileOver)="fileOver($event)" (onFileLeave)="fileLeave($event)">
<ng-template ngx-file-drop-content-tmp let-openFileSelector="openFileSelector">
<button type="button" (click)="openFileSelector()">Drop Files to Upload</button>
</ng-template>
</ngx-file-drop>
<div class="upload-table">
<table id="table1" class="center">
<tbody class="upload-name-style">
<tr *ngFor="let item of files; let i=index">
<td> <input kendoTextBox [(ngModel)]="item.relativePath" style="width: 350px" /></td>
<td>
<kendo-dropdownlist style="width:350px" [(ngModel)]="item.selectedDocumentItem"
[data]="DocumentTypes" [defaultItem]="defaultItem" [filterable]="false" textField="Name"
valueField="Id">
</kendo-dropdownlist>
</td>
<td>
<kendo-datepicker style="width: 200px" [format]="'dd MMM, yyyy'"
[(ngModel)]="item.selectedDate"></kendo-datepicker>
</td>
<td> <button class="btn btn-default" (click)="deleteRow(i)"><i class="fa fa-trash"></i>Delete
</button></td>
</tr>
</tbody>
</table>
</div>
<div class="wrapper">
<button *ngIf="files.length > 0" type="submit" class="btn btn-primary btn-upload">upload</button>
</div>
</div>
</form>
组件
public createDocument() {
this.files.forEach(element => {
this.uploadDocument = <IDocument>{
id: 5508,
documentTypeId: element.selectedDocumentItem.Id ,
name: element.relativePath,
documentDate: element.selectedDate
};
});
}
答案 0 :(得分:1)
您唯一需要的是使用引用变量,看看是否有效。我在stackblitz中放了一个简单的例子。因为您只想知道是否具有有效,所以可以使用简单必需的。我的堆积如山
更新:在表单中,禁用了按钮提交
@Component({
selector: 'my-app',
template: `
<div class="example-wrapper">
<form #myForm="ngForm">
<div *ngFor="let item of files; let i=index">
<p>T-shirt size:</p>
<kendo-dropdownlist name="select{{i}}" #name="ngModel" [(ngModel)]="value[i]" [data]="listItems" required>
</kendo-dropdownlist>
<span *ngIf="name.invalid">*</span>
</div>
<button [disabled]="myForm.invalid">submit</button>
</form>
</div>
`
})
export class AppComponent {
public listItems: Array<string> = ["X-Small", "Small", "Medium", "Large", "X-Large", "2X-Large"];
value=[]
files=[{value:''},{value:''},{value:''}]
}
简要说明(但您有docs)
当我们有一个[[ngModel)]时,我们可以使用模板引用来引用输入。如果我们等于对ngModel #name="ngModel"
的模板引用,则可以在{html}中使用模板变量和ngModel的所有属性(无效,已触摸...),方式为name.invalid
,{{ 1}} ...
啊!,不用担心您会把“相同”的参考变量放进去,如果模型的变量不相等,Angular会理解它们是不同的变量。
注意:就个人而言,我建议您使用ReactiveForms和FormArray,但这只是一个意见
更新2 确实存在问题,因为您无法在要修改的列表上循环。您有name.touched
,并且正在更改*ngFor="let item of files"
。诀窍是在files
上进行迭代-或在代码' '.repeat(files.length).split('')
中创建一个数组,然后可以进行this.iterator=new Array(this.files.length)
[(ngModel)]在文件上[i] .selectedDocumentItem.Id
请参见stackblitz和代码
<tr *ngFor="let t of iterator;let i=index">
如果要用作[ngModel] = files [i] .selectedDocumentItem(一个对象),则需要创建一个customError指令。看起来像
<div class="example-wrapper">
<form #myForm="ngForm">
<!-- other way is <tr *ngFor="let t of iterator;let i=index"> -->
<tr *ngFor="let t of ' '.repeat(files.length).split(''); let i=index">
<td>
<kendo-dropdownlist name="select{{i}}" #name="ngModel" [(ngModel)]="files[i].selectedDocumentItem.Id" [defaultItem]="files[i].selectedDocumentItem.id"
[data]="DocumentTypes" [valuePrimitive]="true" textField="Name" valueField="Id" required>
</kendo-dropdownlist>
<span *ngIf="name.invalid">*</span>
</td>
</tr>
<button [disabled]="myForm.invalid">submit</button>
</form>
</div>
现在我们的下拉菜单就像
@Directive({
selector: '[requiredId]',
providers: [{provide: NG_VALIDATORS, useExisting: RequiredIdDirective, multi: true}]
})
export class RequiredIdDirective implements Validator {
validate(control: AbstractControl): {[key: string]: any} | null {
return control.value.Id?null:{required:true}
}
}
查看新的stackblitz