angular - 如何从输入框获取值到Angular中的方法
问题描述
我学习了 Angular,我有这个 html 代码,当我按下回车键时,该方法getBookById
被调用
这是 search-books.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { BookService } from '../Services/book.service'
import { BookItem } from '../Services/BookItem';
@Component({
selector: 'app-add-contact',
templateUrl: './search-books.component.html',
styleUrls: ['./search-books.component.scss']
})
export class SearchBooksComponent {
public name: string;
public type: number;
public number: number;
private formBuilder: FormBuilder;
private location: Location;
bookItems: BookItem[];
public bookGroup = new FormGroup({
title: new FormControl(''),
author: new FormControl(''),
genre: new FormControl(''),
price: new FormControl(''),
});
constructor(private bookService: BookService) {
}
/** GET all books from server. */
getBookItems() {
this.bookService.getBookItems().subscribe(bookItems => this.bookItems = bookItems);
};
/** GET book by id from server. */
getBookById(value: string) {
this.bookService.getBookItem(value).subscribe(bookItems => this.bookItems = bookItems);
};
}
问题是 (value: string) 总是 "" 空
这是 search-books.component.html
<mat-card class="form-container" >
<mat-card-header><mat-card-title>Search for books</mat-card-title></mat-card-header>
<form [formGroup]="bookGroup" class="form-container">
<mat-form-field>
<input type="text" matInput placeholder="Title" formControlName="title" #box (keyup.enter)="getBookById(box.value)"> <p>{{value}}</p>
</mat-form-field>
<mat-form-field>
<input type="text" matInput placeholder="Author" formControlName="author" #box (keyup.enter)="getAutor(box.value)"> <p>{{value}}</p>
</mat-form-field>
<mat-form-field>
<input type="text" matInput placeholder="Genre" formControlName="genre" #box (keyup.enter)="getGenre(box.value)"> <p>{{value}}</p>
</mat-form-field>
<mat-form-field>
<input type="text" matInput placeholder="Price" formControlName="price" #box (keyup.enter)="getPrice(box.value)"> <p>{{value}}</p>
</mat-form-field>
</form>
</mat-card>
<mat-card *ngFor="let book of bookItems">
<mat-card-header >
<mat-card-title>{{book.title | titlecase}}</mat-card-title>
<mat-card-subtitle>{{book.description}}</mat-card-subtitle>
<mat-card-subtitle>{{book.author}}</mat-card-subtitle>
<mat-card-subtitle>{{book.genre}}</mat-card-subtitle>
<mat-card-subtitle>{{book.publish_date}}</mat-card-subtitle>
<mat-card-subtitle>{{book.price}}</mat-card-subtitle>
</mat-card-header>
</mat-card>
解决方案
当您使用formGroup
时,您可以通过这样做来获得价值getBookById(bookGroup.value.title)
。所以你的输入应该是:
<input type="text"
matInput
placeholder="Title"
formControlName="title"
(keyup.enter)="getBookById(bookGroup.value.title)">
在这种情况下,响应式表单可能不是必需的,但在您学习的过程中,这是一个很好的例子。我邀请您阅读Angular 文档以了解它是如何工作的。
推荐阅读
- asp.net-core - 在 ASP.Net Core 5.0 中,如何将旧的 .aspx webform 路由到 IIS 中托管的控制器操作
- google-apps-script - 如何在列中循环,当某个值被命中时,会根据同一行中的值发送一封电子邮件,并针对设定的范围执行此操作?
- reactjs - 如何使用 Swiper.js 和 React 自定义动态分页子弹样式的过渡动画
- python - 你如何使用 python 找到 Discord.exe?
- gstreamer - 如果 rtph265pay 在 rtph265depay 之前启动,则 GStreamer rtph265pay/rtph265depay 不起作用
- java - 有没有办法将地址与 Google Places API 分开以发表声明?
- c# - 按顺序执行锁(使用 ConcurrentQueue 和锁)
- ios - 以编程方式创建的 UIImageView 和 UITextViews 未在子 UIViewController 中更新
- python-3.x - Python中已知系数的曲线拟合
- r - 编写用于从字符串中提取的正则表达式