gpt4 book ai didi

Angular 'keypress' 事件未给出最新的文本框值

转载 作者:行者123 更新时间:2023-12-02 17:57:56 28 4
gpt4 key购买 nike

我有一个场景,我想通过进行 Http REST 调用来获取匹配的字符串,将每次击键时输入框中出现的字符串发送到服务器。但它没有按我的预期工作,例如,假设输入框中的当前值为“modul”,如果我在“modul”后输入字符“e”,我会期望传递给 searchDepartmentsWithName 方法将为“module”,但它给出的是当前值“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);
}
);
}

最佳答案

您应该使用 keyup 而不是 keypress

(keyup)="searchDepartmentsWithName($event.target.value)"

关于Angular 'keypress' 事件未给出最新的文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50659566/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com