我有这样的场景,我希望通过进行Http REST调用来获取匹配的字符串,在每次击键时将输入框中的字符串发送到服务器。但它没有像我预期的那样工作,比如让我们说输入框中的当前值是'modul',如果我在'modul'之后输入字符'e',我希望传递给searchDepartmentsWithName
的值
方法将是'模块',但它提供当前值'modul'而不是发送到服务器的修改值'module',并返回错误的匹配字符串。简而言之,传递给searchDepartmentsWithName
方法的值始终是修改前的值。我们如何解决这个问题,或者这是否是正确的做法?
searchnox.component.html
<body>
<input type="text" placeholder="enter department name here.." (keypress)="searchDepartmentsWithName($event.target.value)" class="ticketinginput" />
<div *ngFor="let department of searchResults; let i = index">
<p id="result" href="#" (click)="onClick(department)">{{ department.departmentName }}</p>
</div>
</body>
searchbox.component.ts
searchDepartmentsWithName(departmentName: string) {
this.serverService.searchDepartment(departmentName).subscribe( (response) => {
console.log(response);
},
(error) => console.log(error)
);
this.prepareSearchResults();
}
server.service.ts
searchDepartment(name: string) {
this.departmentList = new Array();
const url = `http://localhost:8082/request/searchDepartmentsByName/${name}`;
return this.http.get(url).map(
(response: Response) => {
const data = response.json();
for (const temp of data) {
console.log(temp);
const department: IDepartment = <IDepartment> temp;
this.departmentList.push(department);
console.log('pushing department' + department.departmentName);
}
this.departmentListEventEmitter.emit(this.departmentList);
console.log(this.departmentList.length);
}
);
}
答案 0 :(得分:2)
您应该使用keyup而不是keypress
(keyup)="searchDepartmentsWithName($event.target.value)"