Skip to content

Instantly share code, notes, and snippets.

@raffaele-abramini
Created November 18, 2017 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raffaele-abramini/fbf4a54685c93ba53e25fe203cca542e to your computer and use it in GitHub Desktop.
Save raffaele-abramini/fbf4a54685c93ba53e25fe203cca542e to your computer and use it in GitHub Desktop.
Angular 4 - DOM element ref - 3
// Import ElementRef and ViewChild
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
// Get the DOM element using the ref name from the template and define
// the component property myInput as an ElementRef instance.
@ViewChild('myInput') myInput: ElementRef;
constructor() { }
ngOnInit() {}
doSomething() {
// The ElementeRef instances allows you to access the DOM component via the
// 'nativeElement' property.
const value = this.myInput.nativeElement.value;
console.log(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment