Created
November 18, 2017 09:46
-
-
Save raffaele-abramini/fbf4a54685c93ba53e25fe203cca542e to your computer and use it in GitHub Desktop.
Angular 4 - DOM element ref - 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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