angular - 如何在“输入”之间传递信息
问题描述
我有两个组件“输入”。如何将输入的数据从第一个“输入”(全名)传输到第二个(短名)。
<form [formGroup]="form">
<div class="row">
<div class="input-field col s12 m6">
<input formControlName="full_name" id="full_name" type="text">
<label for="full_name">Full Name</label>
</div>
<div class="input-field col s12 m6">
<input formControlName="short_name" id="short_name" type="text">
<label for="short_name">Short Name</label>
</div>
</div>
</form>
解决方案
您正在使用 ReactiveForm 只需订阅 full_name 控件的事件
import { Component } from '@angular/core';
import {FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
form = new FormGroup({
full_name: new FormControl(),
short_name: new FormControl()
})
constructor() {
this.form.get('full_name').valueChanges.subscribe(fullName => this.form.get('short_name').setValue(fullName));
}
}
您可以使用 debounceTime 等到更新前的给定时间
import { Component } from '@angular/core';
import {FormGroup, FormControl } from '@angular/forms';
import { debounceTime } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
form = new FormGroup({
full_name: new FormControl(),
short_name: new FormControl()
})
constructor() {
this.form.get('full_name').valueChanges.pipe(debounceTime(500)).subscribe(fullName => this.form.get('short_name').setValue(fullName));
}
}
推荐阅读
- python - 重塑数据框的最有效方法是什么
- css - WordPress 中的 CSS:定制器与子主题
- html - 如何使用多个
带角 - c - C中的后缀增量和前缀运算符
- python - OpenVPN 连接 - 通过 Python 的状态?[视窗 10]
- c# - Xamarin iOS 可用 WiFi 网络列表
- uwp - 通用 Windows 平台 (UWP) 在 Process.StandardOutput.ReadToEnd 上挂起
- node.js - 为什么我需要在 package.json 上有一个构建脚本才能在 Now 上部署一些东西?我该如何解决?
- node.js - 基于其他字段的 Mongoose 高级模式/动态“设置”字段
- reactjs - Kafka 上的反应 GUI