Angular 9:一个表单中有多个ngb-typeahead?
创始人
2024-10-18 14:00:58
0

在Angular 9中,你可以在一个表单中使用多个ngb-typeahead。以下是一个示例解决方法:

首先,确保你已经正确安装了ng-bootstrap和ngx-bootstrap。

接下来,创建一个新的Angular组件,并在组件的HTML模板中添加表单和多个ngb-typeahead。

在组件的TS文件中,定义search1和search2函数来处理ngb-typeahead的搜索逻辑。

import { Component } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent {
  selectedItem1: any;
  selectedItem2: any;

  search1 = (text$: Observable) => {
    const searchSubject = new Subject();

    text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      switchMap(term => {
        // Perform your search logic here for Typeahead 1
        // You can make an HTTP request or search an array of items
        const results = this.searchItems(term); // Replace with your own search function

        return results;
      })
    ).subscribe(searchSubject);

    return searchSubject;
  }

  search2 = (text$: Observable) => {
    const searchSubject = new Subject();

    text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      switchMap(term => {
        // Perform your search logic here for Typeahead 2
        // You can make an HTTP request or search an array of items
        const results = this.searchItems(term); // Replace with your own search function

        return results;
      })
    ).subscribe(searchSubject);

    return searchSubject;
  }

  onSelectItem1(item: any) {
    // Handle the selected item for Typeahead 1
    console.log('Selected item for Typeahead 1:', item);
  }

  onSelectItem2(item: any) {
    // Handle the selected item for Typeahead 2
    console.log('Selected item for Typeahead 2:', item);
  }

  searchItems(term: string): Observable {
    // Replace this with your own search logic
    // You can make an HTTP request or search an array of items
    // and return the results as an Observable
    const items = [
      { name: 'Item 1' },
      { name: 'Item 2' },
      { name: 'Item 3' }
    ];

    const filteredItems = items.filter(item => item.name.toLowerCase().includes(term.toLowerCase()));

    return of(filteredItems);
  }
}

在上述示例中,我们使用了RxJS的操作符来处理输入的搜索逻辑。通过debounceTime和distinctUntilChanged操作符,我们可以限制对搜索功能的频繁调用。switchMap操作符用于在每次输入变化时,取消之前的搜索请求并发起新的搜索请求。

同时,我们还定义了onSelectItem1和onSelectItem2函数来处理当用户选择一个项时的逻辑。你可以在这些函数中添加你自己的代码来处理所选项的操作。

最后,我们还定义了searchItems函数来模拟搜索逻辑。你可以根据自己的需求替换这个函数,例如使用HTTP请求从服务器获取搜索结果。

希望这个示例能帮助你解决多个ngb-typeahead问题!

相关内容

热门资讯

iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
安卓系统怎么连不上carlif... 安卓系统无法连接CarLife的原因及解决方法随着智能手机的普及,CarLife这一车载互联功能为驾...
oppo手机安卓系统换成苹果系... OPPO手机安卓系统换成苹果系统:现实吗?如何操作?随着智能手机市场的不断发展,用户对于手机系统的需...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...
安卓平板改windows 系统... 你有没有想过,你的安卓平板电脑是不是也能变身成Windows系统的超级英雄呢?想象在同一个设备上,你...
安卓系统上滑按键,便捷生活与高... 你有没有发现,现在手机屏幕越来越大,操作起来却越来越方便了呢?这都得归功于安卓系统上的那些神奇的上滑...
安卓系统连接耳机模式,蓝牙、有... 亲爱的手机控们,你们有没有遇到过这种情况:手机突然变成了“耳机模式”,明明耳机没插,声音却只从耳机孔...
希沃系统怎么装安卓系统,解锁更... 亲爱的读者们,你是否也像我一样,对希沃一体机上的安卓系统充满了好奇呢?想象在教室里,你的希沃一体机不...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...