是否可以从 ValueChanges 切换到 (blur) 以发出请求?
Is it possible to swap from ValueChanges to (blur) in order to make a request?
我问这个是因为我开始学习一些基本的 angular 课程,并且我在使用 ValueChanges 输入时使用一些过滤器将请求发送到 firebase:
这是用于搜索的输入:
<input
type="search"
placeholder="Teclee un proveedor..."
[formControl]="campoBusqueda"
/>
这是我在 ngOnInit()
中使用的代码:
this.campoBusqueda = new FormControl();
this.campoBusqueda.valueChanges.subscribe(term => {
this.busqueda = term;
this.cargando = true;
if (this.busqueda.length !== 0) {
this.proveedoresService
.getProveedoresSearch(this.busqueda)
.subscribe(proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
if (this.proveedores.length < 1 && this.busqueda.length >= 1) {
this.noresultados = true;
this.proveedoresService
.getProveedores()
.subscribe( proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
});
} else {
this.noresultados = false;
}
});
this.cargando = false;
this.resultados = true;
} else {
this.proveedores = [];
this.cargando = false;
this.resultados = false;
}
});
现在我想知道是否可以使用 (blur):
<input
type="search"
placeholder="Teclee un proveedor..."
[formControl]="campoBusqueda"
(blur)="myNewSearchMethod()"
/>
在用户从输入中获得焦点后发出请求,而不是每次用户在输入中键入内容时发出请求。
我正在使用的 myNewSearchMethod()
是:
myNewSearchMethod() {
this.campoBusqueda = new FormControl();
this.busqueda = this.campoBusqueda.value;
this.cargando = true;
if (this.busqueda.length !== 0) {
this.proveedoresService
.getProveedoresSearch(this.busqueda)
.subscribe(proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
if (this.proveedores.length < 1 && this.busqueda.length >= 1) {
this.noresultados = true;
this.proveedoresService
.getProveedores()
.subscribe( proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
});
} else {
this.noresultados = false;
}
});
this.cargando = false;
this.resultados = true;
} else {
this.proveedores = [];
this.cargando = false;
this.resultados = false;
};
}
现在的问题是,当我使用(模糊)时,输入的值在按 Tab 键或单击输入后消失,当它到达第一个时给出空值,如果 if (this.busqueda.length !== 0) {
给出控制台中的下一个堆栈跟踪:
ProveedoresComponent.html:5 ERROR TypeError: Cannot read property 'length' of null
at ProveedoresComponent.push../src/app/proveedores/proveedores/proveedores.component.ts.ProveedoresComponent.myNewSearchMethod (proveedores.component.ts:170)
at Object.eval [as handleEvent] (ProveedoresComponent.html:9)
at handleEvent (core.js:23097)
at callWithDebugContext (core.js:24167)
at Object.debugHandleEvent [as handleEvent] (core.js:23894)
at dispatchEvent (core.js:20546)
at core.js:20993
at HTMLInputElement.<anonymous> (platform-browser.js:993)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17280)
是的,它绝对适合您。您只需要从 .ts 中删除 vlaueChanges
订阅,然后像这样调用您的搜索功能
在您的 .ts 文件中只保留此代码
searchForm : FormGroup;
this.searchForm = new FormGroup({
searchField : new FormControl('')
})
myNewSearchMethod(){
// This condition will check if there is some value in `searchField` or not
if(this.searchForm.get('searchField ').value){
// call your search function here
}
}
<input
type="search"
placeholder="Teclee un proveedor..."
[formControl]="campoBusqueda"
(blur)="myNewSearchMethod()"
/>
使用updateOn FormHooks
Reports the update strategy of the AbstractControl (meaning the event
on which the control updates itself). Possible values: 'change' |
'blur' | 'submit' Default value: 'change
component.ts
campoBusqueda = new FormControl('',{
updateOn:'blur'
});
我问这个是因为我开始学习一些基本的 angular 课程,并且我在使用 ValueChanges 输入时使用一些过滤器将请求发送到 firebase:
这是用于搜索的输入:
<input
type="search"
placeholder="Teclee un proveedor..."
[formControl]="campoBusqueda"
/>
这是我在 ngOnInit()
中使用的代码:
this.campoBusqueda = new FormControl();
this.campoBusqueda.valueChanges.subscribe(term => {
this.busqueda = term;
this.cargando = true;
if (this.busqueda.length !== 0) {
this.proveedoresService
.getProveedoresSearch(this.busqueda)
.subscribe(proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
if (this.proveedores.length < 1 && this.busqueda.length >= 1) {
this.noresultados = true;
this.proveedoresService
.getProveedores()
.subscribe( proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
});
} else {
this.noresultados = false;
}
});
this.cargando = false;
this.resultados = true;
} else {
this.proveedores = [];
this.cargando = false;
this.resultados = false;
}
});
现在我想知道是否可以使用 (blur):
<input
type="search"
placeholder="Teclee un proveedor..."
[formControl]="campoBusqueda"
(blur)="myNewSearchMethod()"
/>
在用户从输入中获得焦点后发出请求,而不是每次用户在输入中键入内容时发出请求。
我正在使用的 myNewSearchMethod()
是:
myNewSearchMethod() {
this.campoBusqueda = new FormControl();
this.busqueda = this.campoBusqueda.value;
this.cargando = true;
if (this.busqueda.length !== 0) {
this.proveedoresService
.getProveedoresSearch(this.busqueda)
.subscribe(proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
if (this.proveedores.length < 1 && this.busqueda.length >= 1) {
this.noresultados = true;
this.proveedoresService
.getProveedores()
.subscribe( proveedores => {
this.proveedores = [];
// tslint:disable-next-line: forin
for (const id$ in proveedores) {
const p = proveedores[id$];
p.id$ = id$;
this.proveedores.push(proveedores[id$]);
}
});
} else {
this.noresultados = false;
}
});
this.cargando = false;
this.resultados = true;
} else {
this.proveedores = [];
this.cargando = false;
this.resultados = false;
};
}
现在的问题是,当我使用(模糊)时,输入的值在按 Tab 键或单击输入后消失,当它到达第一个时给出空值,如果 if (this.busqueda.length !== 0) {
给出控制台中的下一个堆栈跟踪:
ProveedoresComponent.html:5 ERROR TypeError: Cannot read property 'length' of null
at ProveedoresComponent.push../src/app/proveedores/proveedores/proveedores.component.ts.ProveedoresComponent.myNewSearchMethod (proveedores.component.ts:170)
at Object.eval [as handleEvent] (ProveedoresComponent.html:9)
at handleEvent (core.js:23097)
at callWithDebugContext (core.js:24167)
at Object.debugHandleEvent [as handleEvent] (core.js:23894)
at dispatchEvent (core.js:20546)
at core.js:20993
at HTMLInputElement.<anonymous> (platform-browser.js:993)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17280)
是的,它绝对适合您。您只需要从 .ts 中删除 vlaueChanges
订阅,然后像这样调用您的搜索功能
在您的 .ts 文件中只保留此代码
searchForm : FormGroup;
this.searchForm = new FormGroup({
searchField : new FormControl('')
})
myNewSearchMethod(){
// This condition will check if there is some value in `searchField` or not
if(this.searchForm.get('searchField ').value){
// call your search function here
}
}
<input
type="search"
placeholder="Teclee un proveedor..."
[formControl]="campoBusqueda"
(blur)="myNewSearchMethod()"
/>
使用updateOn FormHooks
Reports the update strategy of the AbstractControl (meaning the event on which the control updates itself). Possible values: 'change' | 'blur' | 'submit' Default value: 'change
component.ts
campoBusqueda = new FormControl('',{
updateOn:'blur'
});