html - 如何做一个 AngularTS 工作搜索框?
问题描述
我需要制作一个有效的搜索框。此代码不起作用。你能帮我修一下吗?
html:
<form #f="ngForm" (ngSubmit)="onSubmit(f)" #searchForm="ngForm">
<input type="search" placeholder="Search...">
</form>
搜索表单组件:
@Component({
selector: 'app-search-form',
templateUrl: './search-form.component.html',
styleUrls: ['./search-form.component.css']
})
export class SearchFormComponent {
users: User[];
private s: String;
constructor(private route: ActivatedRoute,
private router: Router,
private userService: UserService) { }
onSubmit(f: NgForm) {
this.userService.findByFS(f.value).subscribe(data => {
this.users = data;})
}
}
解决方案
我修改了你的代码。它应该像那样工作。您可以在https://angular.io/api/forms/NgForm查看使用情况。
模板:
<form #f="ngForm" (ngSubmit)="onSubmit(f)">
<input name="search" type="search" ngModel placeholder="Search...">
</form>
零件:
@Component({
selector: 'app-search-form',
templateUrl: './search-form.component.html',
styleUrls: ['./search-form.component.css']
})
export class SearchFormComponent {
users: User[];
constructor(private userService: UserService) {}
onSubmit(f: NgForm) {
this.userService.findByFS(f.value.search).subscribe(data => this.users = data);
}
}
推荐阅读
- java - JavaFx 图形窗口缩放问题
- scala - Spark数据框中的结构解析数组
- r - R:使用 sapply 或 str_replace_all 而不是 FindReplace
- nginx - Nginx 坏网关烧瓶 + UWSGI
- python - 第一个子图(x 轴损坏的 2 个子图)未可视化
- android - 如何在 webview 中加载 .rtf 文件?
- php - 如何在安装期间解决 Magento 2(2.4 版本)localhost 500 服务器错误
- python-3.x - Matplotlib MultiCursor 未显示在多个子图上
- java - How can i center textviews in a table row that is constantly being generated dynamically?
- javascript - VueJS:基于 v-select 项目的动态道具