Angular 8依赖下拉框的输入框
创始人
2024-10-17 23:01:26
0

在Angular 8中,可以使用Reactive Forms和Angular Material来创建一个依赖下拉框的输入框。下面是一个代码示例:

首先,安装必要的依赖:

npm install @angular/material @angular/cdk @angular/forms

然后,导入相关的模块和组件:

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    ReactiveFormsModule,
    MatFormFieldModule,
    MatInputModule,
    MatSelectModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts:

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
  selector: 'app-root',
  template: `
    
      Country
      
        {{ country }}
      
    
    
City {{ city }} ` }) export class AppComponent { countryControl = new FormControl(); cityControl = new FormControl(); countries = ['USA', 'Canada', 'Australia']; cities = { 'USA': ['New York', 'Los Angeles', 'Chicago'], 'Canada': ['Toronto', 'Montreal', 'Vancouver'], 'Australia': ['Sydney', 'Melbourne', 'Brisbane'] }; filteredCities: string[]; constructor() { this.countryControl.valueChanges.subscribe(country => { this.filteredCities = this.cities[country]; }); } }

在上面的示例中,我们使用了FormControl来处理输入框的值。当选择一个国家时,相应的城市选项将被过滤并显示在自动完成的下拉框中。

希望这个示例能够帮助到你!

相关内容

热门资讯

第6分钟教程!微乐小程序免费黑... 第6分钟教程!微乐小程序免费黑科技,微乐小程序游戏破解器苹果系统(开挂)一直是有挂(2026)1、实...
第三方辅助!微乐小程序黑科技,... 第三方辅助!微乐小程序黑科技,提高微乐运气的方法(透视)总是是有挂(哔哩哔哩)1、提高微乐运气的方法...
九分钟教程!微乐小程序黑科技,... 九分钟教程!微乐小程序黑科技,微乐自建房插件详细教程(开挂)其实有挂(2026)1、微乐小程序黑科技...
2026版教学!微乐小程序黑科... 2026版教学!微乐小程序黑科技,小程序微乐辅助器免费版(透视)好像真的有挂(哔哩哔哩)小程序微乐辅...
六分钟教程!微乐小程序黑科技,... 六分钟教程!微乐小程序黑科技,微乐自建房怎么提高运气(开挂)都是真的是有挂(2026)1、首先打开微...
透视系统!微乐小程序黑科技,陕... 透视系统!微乐小程序黑科技,陕西微乐小程序修改器(透视)原来有挂(哔哩哔哩)1、不需要AI权限,帮助...
第4分钟下载!微乐小程序黑科技... 第4分钟下载!微乐小程序黑科技,微乐自建房辅助软件下载(开挂)好像真的是有挂(2026)1、许多玩家...
在玩家背景下!微乐小程序黑科技... 在玩家背景下!微乐小程序黑科技,微乐小程序辅助器出租(透视)都是是真的挂(哔哩哔哩)暗藏猫腻,小编详...
一分钟开挂!微乐小程序黑科技,... 一分钟开挂!微乐小程序黑科技,微乐科技软件下载(开挂)竟然是有挂(2026)1、微乐小程序黑科技免费...
推出新举措!微乐小程序免费黑科... 推出新举措!微乐小程序免费黑科技,微信小程序怎么开挂(透视)竟然真的是有挂(哔哩哔哩)1、每一步都需...