forms - 角度关注表单控制
问题描述
我有以下输入:
<input
#participantInput="ngModel"
id="participantInput"
pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"
class="cell small-7"
placeholder="John.Doe@gmail.com"
[(ngModel)]="newParticipantMail"
(keyup.enter)="addParticipant()"
style="height:10%;">
因为#participantInput="ngModel"
我无法像这样检索 elementRef:
@ViewChild('participantInput') participantInput: ElementRef;
那么,我怎样才能将重点放在那个领域呢?
解决方案
您可以像这样添加另一个模板变量:
<input
#participantInput="ngModel"
#participantRef
id="participantInput"
pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"
class="cell small-7"
placeholder="John.Doe@gmail.com"
[(ngModel)]="newParticipantMail"
(keyup.enter)="addParticipant()"
style="height:10%;">
然后您可以将其访问到您的组件中
@ViewChild('participantRef') participantRef: ElementRef;
显然你必须等待视图初始化,例如实现AfterViewInit
接口
推荐阅读
- string - 连接从 bash 中的函数返回的字符串
- regex - 正则表达式:3 位递增顺序和相同的数字
- ruby-on-rails - ActionController::RoutingError (没有路由匹配 [GET] "/assets/images/logo.png"):
- javascript - Javascript 用来自 Array 的子字符串替换字符串
- reporting-services - ssrs:如何从左到右替换垂直轴(Y)
- composer-php - 自动创建 Composer 命令以将 TYPO3 升级到下一个主要版本
- arrays - SAS 模拟随机变量
- xamarin - 使用在异步方法中打开新页面的代码,我是否需要在主线程上运行它,如果需要,我该怎么做?
- python - 查看R中的代码,基于特定名称从网站下载图片的问题
- python - 使用 Python 在 1 页上发布 2 个表单(Webscraping)