【发布时间】:2018-09-28 05:42:33
【问题描述】:
我有多个输入字段,添加和更改在特定字段上工作正常,但是当进入错误消息部分时,如果一个字段中有输入字段错误,它会显示在所有其他字段中。但是,我希望为该特定字段显示错误。
HTML:
<md-card-content>
<ul class="listClass">
<li *ngFor="let media of videos; let i = index ">
<div>
<input type="text" name="{{media._id}}[i]" id="{{media._id}}[i]" class="form-control form-textbox input-text" [(ngModel)]="media.editText" #editText pattern="/^(ftp|http|https):\/\/[^ ]+$/" style="width: 58%;margin-left: 1%;">
</div>
<div *ngIf="errorMsg" style="color:red">
{{errorMsg}}
</div>
<p class="inputimg" style="float: right;display: inline-block">
<label *ngIf="media._id" class="img_change" (click)="change($event,media)" style="width: 100px;">Change Link</label>
<label *ngIf="!media._id" class="img_change" (click)="changetext($event,media)" >Add Link</label>
</p>
</li>
</ul>
</md-card-content>
TS:
change(event: any, media) {
if (media.editText.indexOf('https://www.youtube.com/embed') != -1) {
this.errorMsg="";
if (!media._id) {
var data:any = {
pin_id: this.pin_id,
media_type: "video",
image_path: media.editText
}
this.ApiService
.addLinkMedia(data)
.subscribe(
media => {
})
} else if(media._id) {
var data:any = {
media_id: media._id,
image_path: media.editText
}
this.ApiService
.addLinkMedia(data)
.subscribe(
media => {
this.loadMedias()
}, error => {
})
}
} else {
this.errorMsg = "Please enter valid URL";
}
}
这里我没有使用任何表单验证。
【问题讨论】:
-
您能否在其他输入字段的位置显示更多的 html 代码或显示错误时的外观。如果您在每个字段下都有`*ngIf="errorMsg",那么它会显示在所有字段上,您必须为每个字段或至少一个数组使用不同的变量,或者更好的角度默认表单验证工具。
-
你能举例说明你做了什么吗
-
这是我提供视频链接的唯一输入字段
-
你在使用 ng-repeat 吗?
-
@Sravan 不,我只使用了这么多代码
标签: html angular typescript