angular2 kullanarak söz konusu giriş alanı için gösterilecek hata mesajını nasıl yapılır

oy
1

i ekleyerek ve söz konusu fieds ile iyi çalışıyor değişen, ancak tüm diğer alanlarda gösterilmektedir bir alanda giriş alanı eror varsa, mesaj bölümüne hata gelirken, birden fazla giriş alanlarına sahiptir. Ama, ben hata söz konusu alan için görüntülemek istediğiniz.

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;
    }
}

burada herhangi bir formu doğrulamaları kullanılan sığınak.

Oluştur 18/04/2018 saat 05:39
kaynak kullanıcı
Diğer dillerde...                            


5 cevaplar

oy
3

Depolayan bir değişken, al idarasında mediave görüntüleme hata mesajı uygun şekilde ortam numarası bağlı olarak .

  • Ben atama ediyorum this.errorDiv[media._id] = true;;kullandığım böylece errorDivde * ngIf
  • HTML'de kullandığım *ngIf="errorMsg[media._id] && (errorDiv[media._id])"hata mesajını ve buna göre belirli Kimliği ve ekran hata mesajı denetler hangi

HTML:

<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[media._id] && (errorDiv[media._id])" style="color:red">
    {{errorMsg[media._id]}}
</div>
<p >
    <label *ngIf="media._id" (click)="change($event,media)">Change Link</label>
    <label *ngIf="!media._id" (click)="change($event,media)">Add Link</label>
</p>

Bileşen:

public errorDiv = {};
public errorMsg = {};


    change(event: any, media) {
        if (media.editText.indexOf('https://www.youtube.com/embed') != -1) {
          this.errorMsg[media._id] = "";
          this.errorDiv[media._id] = "";
          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[media._id] = "Please enter valid URL";
          this.errorDiv[media._id] = true;
        }
    }
Cevap 18/04/2018 saat 06:17
kaynak kullanıcı

oy
1

ayrı ayrı her medya nesnesine hata mesajı bağlama deneyin:

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="media.errorMsg" style="color:red">
        `media`.`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) {
      media.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 {
      media.errorMsg = "Please enter valid URL";
    }
}
Cevap 18/04/2018 saat 06:25
kaynak kullanıcı

oy
0

Bu en iyi yoldur girişini kontrol özel bir doğrulayıcı oluşturmaktır, sadece çok kısaca doğru yönde işaret ancak bulundu. Yeni bir yönerge oluşturma ve uygulama modülünde bunu sağlamak.

@Directive({
    selector: '[appValidURL]',
    providers: [{provide: NG_VALIDATORS, useExisting: URLValidatorDirective, multi: true}]
})
export class URLValidatorDirective implements Validator {
    @Input('appValidURL') url: string;

    validate(control: AbstractControl): {[key: string]: any} {
        return this.url && this.url.startsWith("https://www.youtube.com/embed") ? {value: control.value}}: null;
    }
}

Sonra giriş kodunda böyle bir şey kullanın.

<input type="text" name="videourl" id="videourl" class="form-control" required appValidURL [(ngModel)]="media.editText">
<div *ngIf="videourl.invalid && (videourl.dirty || videourl.touched)" class="alert alert-danger">
   <div *ngIf="videourl.errors.required">
        VideoURL is required!
   </div>
   <div *ngIf="videourl.errors.url">
       It must be an embedded youtube video!
   </div>
</div>

Böyle hiç kullanmadım, ama bazı küçük tweeks ile belki çalışmalıdır.

Düzenleme: Bu sadece tek bir giriş alanı içindir, senin bunu nasıl çalıştığını emin değilim, bunun yerine "videoURL" tutarındaki dizi adları kullanmaya tho deneyebilirsiniz.

Cevap 18/04/2018 saat 06:19
kaynak kullanıcı

oy
0

Aşağıdaki kod bakın ve size hatasını fark edecektir. Akım girişi içindir koşulu eklemelisiniz.

   <div [ngClass]="{ 'has-error': form.submitted && !username.valid }">
          <label for="firstName">First Name</label>
          <input type="text" name="firstName" [(ngModel)]="model.firstName" #firstName="ngModel" required />
          <div *ngIf="form.submitted && !firstName.valid">First Name is required  </div>
  </div>
  <div [ngClass]="{ 'has-error': form.submitted && !username.valid }">
          <label for="lastName">Last Name</label>
          <input type="text" name="lastName" [(ngModel)]="model.lastName" #lastName="ngModel" required />
         <div *ngIf="form.submitted && !lastName.valid" >Last Name is required</div>
  </div>
Cevap 18/04/2018 saat 05:55
kaynak kullanıcı

oy
0

Böyle soething yapabilirsiniz. ts dosyada herhangi bir şey yapmadan. Sen doğrulamak ve sadece formu kontrolleri kullanarak Doğrulama mesajlarını gösterebilir.

<input id="name" name="name" class="form-control"
       required minlength="4" appForbiddenName="bob"
       [(ngModel)]="hero.name" #name="ngModel" >

<div *ngIf="name.invalid && (name.dirty || name.touched)"
     class="alert alert-danger">

  <div *ngIf="name.errors.required">
    Name is required.
  </div>
  <div *ngIf="name.errors.minlength">
    Name must be at least 4 characters long.
  </div>
  <div *ngIf="name.errors.forbiddenName">
    Name cannot be Bob.
  </div>

</div>

Açısal alınan resmi doc

Cevap 18/04/2018 saat 05:46
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more