How to console log the text from an input when pressing the button associated with it?

Get support on Angular & Typescript related technology.
Post Reply
admin
Site Admin
Posts: 44

How to console log the text from an input when pressing the button associated with it?

Post by admin » Fri Aug 23, 2019 8:07 pm

app.component.html

Code: Select all

<input placeholder="Text"
      [(ngModel)]="text">

 <button (click)="onClick()">Click me</button>    
app.component.ts

Code: Select all

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  public text: string;
  onClick(){
    console.log(this.text);
  }

}

app.module.ts

Code: Select all

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';


@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
Created a demo here https://stackblitz.com/edit/angular-con ... cussioncom

Post Reply