简体   繁体   English

如何在ngIf中给ngFor变量?

[英]How do I give ngFor variable inside ngIf?

I am having code like this: 我有这样的代码:

<span *ngFor="let track_type of model.content_id.track_types">
    <label class="col-3">{{track_type}} Stream ID*:</label>
    <input type="text" name="stream_id_{{track_type}}" class="col-offset-1 col-3" #stream_id="ngModel" placeholder="{{track_type}} Stream ID" required [(ngModel)]="model.stream_id">
    <span *ngIf="stream_id.invalid && (stream_id.dirty || stream_id.touched)" class="col-offset-2 col-5 text-danger">Stream ID is required</span>
    <br>
</span>

There are two issues: 有两个问题:

  • If I edit one text box, it is affecting all the text boxes. 如果我编辑一个文本框,它将影响所有文本框。
  • It is unable to set the form validation properly 无法正确设置表单验证

How do I resolve these issues? 我该如何解决这些问题? I tried looking at ngTemplateOutlet , but it's not really working as expected. 我尝试查看ngTemplateOutlet ,但实际上并没有按预期工作。

Use a trackByFunction 使用trackByFunction

  trackByFn(index, item) {
    return index; // or item.id
  }

And in your template: 在您的模板中:

<span *ngFor="let track_type of model.content_id.track_types; trackBy: trackByFn">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM