angular - 如何动态过滤角度json文件?
问题描述
我想根据我输入的字母搜索我的 json 文件,它应该被相应地过滤,这是我的 json 文件:
constructor(private service:ServiceService) { }
ngOnInit(): void {
this.x="123";
this.service.getListofTurbines(this.x).subscribe(s=>{
this.rslt=s;
})
使用过滤器它会返回正确的匹配项,但我如何动态搜索它?
解决方案
打字是在控制中发生的动作,因此在角度最好的方法是如果你创建一个FormGroup
和一个FormControl
为了示例,我们将调用您的表单listForm
和控件searchControl
listForm: FormGroup;
...
//this could on ngOnInit declaring the formControl and the formGroup
this.listForm = this.formBuilder.
group({ item: this.formBuilder.array([]),
'searchControl': new FormControl("") });
...
//this could on ngOnInit observing the value changes when typing occurs
this.listForm.controls['searchControl'].valueChanges .pipe(
startwith(''),
debounceTime(500) /*to avoid searching every letter, increase time to reduce searches*/)
.subscribe((value) => {
//and here your perform your search
推荐阅读
- ios - 在 Swift 中对另一个视图控制器执行 Segue 后关闭游戏视图控制器
- swift - 如何快速获取 NSImage 格式和 alpha?
- javascript - 在 HTML5 画布中快速操作 drawImage() 亮度
- android - 我想从 API 数据 Android Java 中显示头像
- c - 为什么 snprintf 在 %s 之后不读取?
- python - CNN中输入的不平衡纵横比
- vb.net - OpenXML 的文档格式
- plsql - 在 PL/SQL 中循环使用游标
- c# - TCPClient & TCPListener | 对等崩溃 c# 重置连接
- html - 我如何制作响应式导航栏 - BootStrap v3