Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a ref to access nativeElement #2007

Closed
its-dibo opened this issue Dec 28, 2019 · 6 comments
Closed

add a ref to access nativeElement #2007

its-dibo opened this issue Dec 28, 2019 · 6 comments

Comments

@its-dibo
Copy link

fields:[
 {
  key:"test",
  type:"input",
  ref:"test"  //adds <input #test >, so we can access this input from our component via @viewChild()
]
@aitboudad
Copy link
Member

not sure if that possible with angular, to get the nativeElement of the input you may rely on the field id instead:

document.querySelector(`#${field.id}`)

@its-dibo
Copy link
Author

but what I mean is to access the element in your component file. not in DOM tree

example:

//my.component.html
<form #formRef>
 <input #elementRef>
</form>

//my.component.ts
class MyComponent {
  @ViewChild("formRef", { static: false }) formRef;
  @ViewChild("elementRef", { static: false }) elementRef;
  test;

//now we can access formRef & elementRef, for example:
onSubmit(){
 //do some stuff to submit the form, or send the data to the server
 this.formRef.reset(); 
 this.test = this.elementRef.nativeElement.value;
 }
}

@aitboudad
Copy link
Member

Currently, there is no straight way to implement this feature, sorry about that 🙏!

Blocked by:

@its-dibo
Copy link
Author

its-dibo commented Dec 29, 2019

I don't talk about dynamically rendering the template, I talk about a feature already supported by Angular

https://itnext.io/working-with-angular-5-template-reference-variable-e5aa59fb9af

https://angular.io/api/core/ElementRef

@aitboudad
Copy link
Member

I know exactly what you were talking about 😃, we create field components dynamically using ComponentFactoryResolver which AFAIK angular doesn't recognize by ViewChild (correct me if I'm wrong).

@vmtzgr
Copy link

vmtzgr commented Apr 24, 2024

Is there a solution available for this now? I need to reference some of my fields (ng-select, ionic inputs...) in a reusable subform...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants