gpt4 book ai didi

javascript - 如何仅在输入模糊时触发 react 形式 valueChanges 订阅并在 Angular 2 中输入 key

转载 作者:太空狗 更新时间:2023-10-29 17:32:23 28 4
gpt4 key购买 nike

我正在开发一种表单,该表单应该仅在输入模糊或按下输入键时更新某些 ui 部分。我是响应式表单的忠实粉丝,我正在尝试找出正确的方法。到目前为止,我还没有找到太多 react 形式的答案。他们中的大多数都专注于模板驱动的表单。到目前为止我得到了这样的东西:

表单组件 动态表单值在运行时发生变化(没有可预测的文件名)

import { Component, Input, Output, EventEmitter, 
ChangeDetectionStrategy } from '@angular/core'
import { FormBuilder, FormGroup, FormControl } from '@angular/forms'
import { Observable } from "rxjs/Observable"
import { DEBUG } from '../../../../config/config'
import * as Debug from 'debug'

// Interfaces
import { Foo } from '../../interfaces/foo'

// Services
import { FooService } from '../../services/foo.service'

// Debug
const debugOff = (...any) => { }, debug = Debug('app:FooCmp')

@Component({
selector: 'foo-data-cmp',
templateUrl: './foo.data.cmp.html',
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrls: ['./foo.data.cmp.css']
})
export class FooDataCmp {

private _foo: Foo[] = null
@Input('foo')
set foo(foo: Foo[]) {
this._foo = foo
DEBUG.input && debug('Input foo:', this._foo)
}
get foo(): Foo[] {
return this._foo
}

// Forms
public fooForm: FormGroup
public fooForm$: Observable<any>
private updatedFoo: any[] // Updated form values

// Subscriptions
private subscriptions: any[] = []

constructor(
private formBuilder: FormBuilder,
private fooService: FooService,
) {
DEBUG.constr && debug('Construct FooDataCmp')
}

ngOnInit() {
DEBUG.init && debug('Initialise FooDataCmp')

// Form
this.initFooForm()
this.subcribeToFormChanges()
}

ngOnDestroy() {
DEBUG.destroy && debug('Destroy FooDataCmp')
this.subscriptions.forEach(sub => sub.unsubscribe())
}

private initFooForm() {
DEBUG.cmp && debug('Initialise foo form')

// Build the form
this.fooForm = this.formBuilder.group(this._foo)

// Prevent undefined error at first keystroke
this.updatedFoo = this._foo
}

private subcribeToFormChanges() {
this.fooForm$ = this.fooForm.valueChanges
let sub = this.fooForm$.subscribe(fooForm => {
DEBUG.cmp && debug('Form changes fooForm', fooForm)
this.updatedFoo = fooForm
})
this.subscriptions.push(sub)
}

/**
* <!> Important step in the data update process
* Update state store.
* Other services / components are subscribed to the state store itself
*/
public refreshAllFooRelatedData () {
DEBUG.cmp && debug('Refresh all foo related data')
DEBUG.cmp && debugOff('Updated foo', this.updatedFoo)
this.fooService.refreshAllFooRelatedData(this.updatedFoo)
}

public refreshAllFooRelatedDataOnEnter (e: KeyboardEvent) {
if (e.keyCode !== 13) {return}
DEBUG.cmp && debug('Refresh all foo related data (on enter)')
DEBUG.cmp && debugOff('Updated foo', this.updatedFoo)
this.fooService.refreshAllFooRelatedData(this.updatedFoo)
}

public debugTemplate() {
DEBUG.render && debug('Render FooDataCmp')
}

}

表单模板

<form [formGroup]="fooForm">
<table class="list expiries">
<td *ngFor="let value of foo; let i = index">
<input type="text" [formControlName]="foo[i]"
(blur)="refreshAllFooRelatedData()"
(keydown)="refreshAllFooRelatedDataOnEnter($event)">
</td>
</table>
</form>

{{ debugTemplate() }}

这是一个不错的解决方案还是我错过了一些技巧?我的问题是这是否是正确的方法。我一直期待找到一些表单 API 选项来处理此任务。我的方法是在表单之上构建,但如果有任何可用于此任务的东西,我宁愿使用完整的表单 api

最佳答案

如果将来有人发现这个,实际上有一些内置于 react 形式的东西,updateOn 选项:

new FormControl('', {updateOn: 'blur'});

这将导致 valueChanges 和验证以及诸如此类的东西只在模糊时触发

关于javascript - 如何仅在输入模糊时触发 react 形式 valueChanges 订阅并在 Angular 2 中输入 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46664010/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com