admin管理员组文章数量:1323023
I am unable to get the reference of nativeElement of my custom Elements. I have a template like this:
<div #testone>
<my-feature-cmp><my-feature-cmp>
<my-feature-cmp><my-feature-cmp>
<my-feature-cmp><my-feature-cmp>
<div>
Code used to access: @ViewChild('testone') el: ElementRef;
I get the element reference when I do this -> console.log(this.el.nativeElement)
Second type of template
<my-feature-cmp></my-feature-cmp>
<my-feature-cmp></my-feature-cmp>
<my-feature-cmp></my-feature-cmp>
Code used to access:
@ViewChildren(MyFeatureCmp) el: MyFeatureCmp;
I get the error for native element when I do this ->
console.log(this.el.nativeElement)
I get the class references and no nativeElement when I do this ->
console.log(this.el)
console.log(this.el.toArray())
The question is how to I access the native element of custom ponent if I want to make changes to the tag attributes. Second, whatever way I access them, If I change the attributes manually for custom ponent will it get detected as a change as well after the change?
I am unable to get the reference of nativeElement of my custom Elements. I have a template like this:
<div #testone>
<my-feature-cmp><my-feature-cmp>
<my-feature-cmp><my-feature-cmp>
<my-feature-cmp><my-feature-cmp>
<div>
Code used to access: @ViewChild('testone') el: ElementRef;
I get the element reference when I do this -> console.log(this.el.nativeElement)
Second type of template
<my-feature-cmp></my-feature-cmp>
<my-feature-cmp></my-feature-cmp>
<my-feature-cmp></my-feature-cmp>
Code used to access:
@ViewChildren(MyFeatureCmp) el: MyFeatureCmp;
I get the error for native element when I do this ->
console.log(this.el.nativeElement)
I get the class references and no nativeElement when I do this ->
console.log(this.el)
console.log(this.el.toArray())
The question is how to I access the native element of custom ponent if I want to make changes to the tag attributes. Second, whatever way I access them, If I change the attributes manually for custom ponent will it get detected as a change as well after the change?
Share Improve this question asked Jul 13, 2017 at 4:54 GaryGary 2,3392 gold badges30 silver badges53 bronze badges 3- If I change the attributes manually - what exactly do you change and what do you want framework to pick up? – Max Koretskyi Commented Jul 13, 2017 at 4:58
- 2 See stackoverflow./questions/39908967/… and stackoverflow./questions/45064576/… – yurzui Commented Jul 13, 2017 at 4:58
-
what exactly do you change
. 1) If I access the ponent class instance's objects say a variable within it will it be change detected automatically? 2) If I get the cmp element ref of<my-feature-cmp>
then can I do this within breaking something somewhere?this.el.first.nativeElement.someatrribute = 'somevalue'
orthis.el.first.nativeElement.style.color = '#ccc'
or add event listeners(somecustomevent)
on fly? – Gary Commented Jul 13, 2017 at 6:04
1 Answer
Reset to default 10The question is how to I access the native element of custom ponent
You have to use read
option:
@ViewChildren(MyFeatureCmp, {read: ElementRef}) el: QueryList<ElementRef>;
console.log(this.el.first.nativeElement)
Also see this answer to learn what we can get using read
.
If I change the attributes manually for custom ponent will it get detected as a change as well after the change?
No, Angular doesn't process changes on DOM elements. For more information on what change detection processes see Everything you need to know about change detection in Angular.
本文标签: javascriptUnable to capture nativeElement for custom angular componentsStack Overflow
版权声明:本文标题:javascript - Unable to capture nativeElement for custom angular components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742111348a2421270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论