Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
¿Hay una forma Angular2 de enfocarse en un campo de entrada?

Al final de un método, borraré algunos campos, lo cual es bastante fácil:

 this.modelname.fieldname1 = ""; this.modelname.fieldname2 = "";

Después de borrar los campos, me gustaría que el cursor apareciera en field1 .

¿Hay una forma Angular2 de hacer esto, o simplemente uso Javascript antiguo?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

No se recomienda acceder a dom directamente en Angular. Angular proporciona abstracciones de Renderer (Angular2) y Renderer 2 (Angular4).

Aquí está cómo hacerlo en Angular 2:

 @Component({ selector: 'my-app', template: ` <div> <input #inp id="myInput" type="text"> <button (click)="setFocus()">Set Focus</button> </div> `, }) export class App { @ViewChild('inp') inp:ElementRef; constructor(private renderer: Renderer) { } setFocus() { this.renderer.invokeElementMethod(this.inp.nativeElement, 'focus'); } }

En Angular4, Renderer está en desuso y se agrega Renderer2. invoqueElementMethod se eliminó y hay una discusión al respecto: https://github.com/angular/angular/issues/13818#issuecomment-297815331

Aquí está Angular 4 vías:

 let onElement = this.renderer2.selectRootElement('#myInput'); onElement.focus();
about 4 years ago · Santiago Trujillo Report

0

Puede usar una variable de referencia de plantilla en la primera entrada, que le permitirá ejecutar .focus() en ella.

En su plantilla, puede agregar #fieldName1 a su etiqueta de entrada (es decir, <input type="text" #fieldName1> )

en tu controlador haz:

 @ViewChild('fieldName1') fieldName1: any;

ahora puede hacer this.fieldName1.nativeElement.focus() en el controlador o fieldName1.focus() en la plantilla.

about 4 years ago · Santiago Trujillo Report

0

Agregando una forma de renderer usando el decorador @ViewChild . Puede omitir el uso del atributo #id como lo menciona @julia arriba.

Se centrará en el elemento de entrada una vez que se inicialice la vista Html:

 <textarea #tasknote name="tasknote"> </textarea>

JS:

 export class MyComponent implements AfterViewInit { @ViewChild('tasknote') input: ElementRef; constructor(private renderer: Renderer2){ } ngAfterViewInit() { //using selectRootElement instead of deprecated invokeElementMethod this.renderer.selectRootElement(this.input["nativeElement"]).focus(); } }

para saber más sobre otras funciones del renderer , siga los documentos oficiales

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!