Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 get dom element

Hi i'm trying to set focus on an input element once the edit button is clicked on an Angular 4 application.

First i tried Renderer2, but it doesn't seem to work for this purpose.

Now I'm trying to get it using @ViewChild, I'm always getting value as undefined.

I event tried implementing AfterViewInit and logging the element immediately once loaded, but still I'm getting 'undefined'. Please help..

        import { Component, OnInit, Input, Output, EventEmitter, Renderer2, ViewChild, AfterViewInit } from '@angular/core';
        import { NgIf } from '@angular/common';
        import { DataService } from '../data.service';
        import { NgForm } from '@angular/forms';

        @Component({
            selector: 'app-artist-list-item',
            template: `
                    <button class="w3-button w3-circle w3-orange" (click)="edit(nameInput)"><i class="fa fa-pencil"></i></button>
                    <span *ngIf="editable">
                        <input  class="name" #nameInput (keyup)="onKey(nameInput.value)" (keypress)="inputEnter($event)" [value]="artist.name">
                        <button class="w3-button w3-green btn-save" (click)="save()">Save</button>
                    </span>
                    <span *ngIf="!editable">
                        {{artist.name}}
                    </span>
            `,
            styleUrls: ['./artist-list-item.component.scss']
        })
        export class ArtistListItemComponent implements OnInit, AfterViewInit {

            @Input() artist: any;
            @Output() onDelete = new EventEmitter();
            @Output() onEdit = new EventEmitter();
            @Output() onSave = new EventEmitter();
            @ViewChild('nameInput') nameInput;

            public editable: boolean = false;
            name: any = '';

            constructor(private Data: DataService, private rd: Renderer2) { }

            ngAfterViewInit() {
                console.log(this.nameInput);
            }

            ngOnInit() {
            }

            edit(el) {
                console.log(el);
                console.log(this.nameInput);
                this.editable = true;
                this.name = this.artist.name;
            }

        }

by the way i have removed the code where i tried to set focus.

            edit(el) {
                console.log(el);
                console.log(this.nameInput);
                this.nameInput.focus();
                this.editable = true;
                this.name = this.artist.name;
            }
like image 421
Bmax Avatar asked Jul 30 '17 03:07

Bmax


People also ask

How does the Angular identify the DOM elements?

We can access the DOM in Angular using different reference types like ElementRef , TemplateRef , ViewRef , ComponentRef and ViewContainerRef . These reference types can be queried from templates using @ViewChild and @ContentChild . Browser's native DOM element can be accessed via ElementRef .

How do I get ElementRef?

Getting ElementRef in Component ClassCreate a template reference variable for the element in the component/directive. Use the template variable to inject the element into component class using the ViewChild or ViewChildren.

What is DOM element in Angular?

DOM stands for Document Object Model. AngularJS's directives are used to bind application data to the attributes of HTML DOM elements.

How can I select an element in a component template?

Add a template reference variable to the component HTML element. Import @ViewChild decorator from @angular/core in component ts file. Use ViewChild decorator to access template reference variable inside the component.


1 Answers

You need to access the nativeElement property on the element you defined within your @ViewChild declaration to manipulate the actual DOM element. Calling @ViewChild alone just creates an ElementRef to an element within the DOM. Hence why you have to access the nativeElement property of the ElementRef to manipulate the DOM element. For example:

ngAfterViewInit() {
    console.log(this.nameInput.nativeElement);
}

This is a great article on DOM Abstractions in Angular.

like image 164
joshrathke Avatar answered Oct 09 '22 13:10

joshrathke