angular - set focus on button whenever it gets enabled
问题描述
In my Angular 8 application i am trying to set focus on mat button as soon as it gets enabled . Enabling button happens after receiving response from service and validating few conditions . Setting focus on mat button dynamically not working is my old question . But for some reason it is not working when i used in my original code. So i am looking for options something like autofocus directive that could set focus when button gets enabled. Is it possible to achieve ?
解决方案
With the following modifications to the answer provided by Beka i was able to set focus on button
export class InputErrorsExample {
@ViewChild('search', {read: ElementRef, static: false}) btn: ElementRef; // to access button in ts
city = new FormControl('');
search: boolean = false;
constructor(private http: HttpClient) {}
fetchCityDetails() {
this.http
.get('https://countriesnow.space/api/v0.1/countries/population/cities')
.subscribe(data => {
if (data) {
this.btn.nativeElement.disabled = false ;
this.btn.nativeElement.focus();
}
});
}
}
推荐阅读
- python - 如何在python中为正十六进制数给出符号+
- python - Pandas - 计算可变窗口大小的滚动累积产品
- sql - 列和行之间的减法和加法
- r - 替换R中字符串中第一次出现的字符
- apache-spark - 如何使用动态键加入 pyspark 数据框
- php - 使用 $_POST 回显大数组中特定子数组的一段代码
- mysql - DB 使用出现问题...由 Flyway 迁移的 SP 会产生 MySQL 语法错误,而相应的原始脚本不会
- python - IndexError:列表索引超出范围 - 填充虚拟变量
- ruby - 为什么 instance_exec 不覆盖 Proc 对象绑定中存在的局部变量?
- c# - 测试程序集从命令提示符而不是从发布管道运行,“没有找到与模式匹配的测试程序集”错误