简体   繁体   English

Angular:使用自定义指令访问基本元素引用

[英]Angular: Accessing base element reference for elements with custom directives

Given the following template: 给定以下模板:

<input my-base-directive #someRef> {{someRef.value}}

How do I make someRef actually reference the HTMLInputelement rather than my-base-directive ? 如何使someRef实际上引用HTMLInputelement而不是my-base-directive I know i can use exportAs on the directive to make that accessible using the refs, but how do I explicitly not get the directive, and instead get the <input> element? 我知道我可以在指令上使用exportAs以使用refs进行访问,但是我如何显式获取指令,而获取<input>元素?

Stackblitz Link: https://stackblitz.com/edit/angular-z6zvbz Stackblitz链接: https ://stackblitz.com/edit/angular-z6zvbz

With this: <input my-base-directive #someRef> {{someRef.value}} 这样: <input my-base-directive #someRef> {{someRef.value}}

someRef will reference the DOM element the input box. someRef将在输入框中引用DOM元素。

To get to the directive you need to use the 'exportAs' in the directive and use it like this: <input my-base-directive #dir="mybasedir" #someRef [value]="name"> {{someRef.value}} 要转到该指令,您需要在指令中使用'exportAs'并按如下方式使用它: <input my-base-directive #dir="mybasedir" #someRef [value]="name"> {{someRef.value}}

Check this stackblitz . 检查此stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM