简体   繁体   English

如何绑定 <input> 到另一个 <input> 在Angular 2中?

[英]How to bind an <input> to another <input> in Angular 2?

TL;DR: binding an input-field to a second input-field results in not being able to get the value of the second input-field through its FormControl, unless the second input-field has been changed directly by the user, in which case the FormControl has the correct value, as expected -- I'm probably making a mistake with the type of binding? TL; DR:将输入字段绑定到第二输入字段会导致无法通过其FormControl获取第二输入字段的值,除非用户直接更改了第二输入字段,在这种情况下,如预期的那样,FormControl具有正确的值-我可能在绑定类型上出错了?

Full explanation: 完整说明:

So I have this form: 所以我有这种形式:

<label for="name">Name:</label>
<input type="text" id="name" [formControl]="artistForm.controls['name']" />

<label for="slug">Slug:</label>
<input type="text" id="slug" [formControl]="artistForm.controls['slug']" />

I want to achieve that whatever a user enters in input#name also appears in input#slug, but going through a slug-pipe or slug-function which creates a URL-friendly version of the name. 我想要实现的是,无论用户在input#name中输入的内容还是出现在input#slug中,但是都要通过slug-pipe或slug-function来创建名称的URL友好版本。

For example: '50 Cent' turns into '50-cent' and 'Bué the Warrior' to 'bue-the-warrior'. 例如:“ 50美分”变成“ 50美分”,“Buéthe Warrior”变成“ bue-the-warrior”。 Pretty much the way slugs in WordPress are generated automatically. WordPress中的子弹几乎是自动生成的。

So I tried binding the value of input#name to input#slug like this: 因此,我尝试将input#name的值绑定到input#slug,如下所示:

<label for="name">Name:</label>
<input type="text" id="name" [formControl]="artistForm.controls['name']" />

<label for="slug">Slug:</label>
<input type="text" id="slug" (value)="{{ artistForm.controls['name'].value | slug }}" [formControl]="artistForm.controls['slug']" />

This works for the input-field directly, but the value-property of the formControl associated with it only gets updated when the input-field has been edited directly. 这直接适用于输入字段,但是与之关联的formControl的值属性仅在直接编辑输入字段时才会更新。

I want to generate a slug from the name-field, but still allow the user to change the value of the slug-field manually. 我想从名称字段生成一个段,但仍允许用户手动更改段的值。 How do I fix this? 我该如何解决?

输入#slug中的FormControl不包含当前值

EDIT: updated to latest Angular-version, didn't solve the problem. 编辑:更新到最新的Angular版本,没有解决问题。

You are using form group. 您正在使用表单组。 So you should do it using forms. 因此,您应该使用表单。 Listen to changes and if there is change with name and slug is not set them update slug 侦听更改,如果名称和子弹未更改,则未设置更新子弹

this.formGroup.valueChanges.subscribe((value)={
  if(!value.slug && value.name) this.formGroup.patch({slug: value.name});
});

Then fix [formControl]="artistForm.controls['name']" to be [formControl]="name" and do the same for slug 然后将[formControl]="artistForm.controls['name']"修复为[formControl]="name"然后对[formControl]="name"执行相同的操作

PS. PS。 Replace formGroup with your property name 用您的属性名称替换formGroup

The answer by Volodymyr Bilyachat helped me a great deal, but it didn't work without some adjustments. Volodymyr Bilyachat的回答对我有很大帮助,但是如果不进行一些调整,它就行不通。 He was totally right that I had to update slug in the typescript file instead of my template. 他完全正确,我必须在打字稿文件而不​​是模板中更新段塞。 When using the valueChanges-observable on the formGroup however, I got this error: 但是,在formGroup上使用valueChanges-observable时,出现此错误:

Maximum call stack size exceeded

Of course, because all changes in the entire form were subscribed to, which results in an endless loop. 当然,因为整个表单中的所有更改都已订阅,这导致了无限循环。 So I played around with the code and this worked flawlessly: 因此,我尝试了一下代码,这完美地工作了:

this.formGroup.get('name').valueChanges.subscribe(value => {
  const slug = slugify(value); // slugify turns a string into a slug
  this.addArtistForm.get('slug').patchValue(slug);
});

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

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