6

I'm using angular7. Just make some textbox and then alert that textbox value. But I can't get the values in typescript. Please help me guys how to proceed more this step.

about.component.html

<input type="text" name="username">
<button (click)="lgbtnclick()" type="button" name="button"></button>

about.component.ts

  import { Component, OnInit } from '@angular/core';
  @Component({
  selector: 'app-about',
  templateUrl: './about.component.html',
  styleUrls: ['./about.component.scss']
  })

  export class AboutComponent implements OnInit {
   name : string;

   constructor() { }

   ngOnInit() {
   }

   lgbtnclick(){
      alert(this.name)
      console.log(Error)
   }
}

alert message:

undefined
9
  • shoe us the html Commented Feb 23, 2019 at 13:05
  • Its now showing Commented Feb 23, 2019 at 13:07
  • use ngmodel to bind textbox value to name varible .name is undefined it's not initialized Commented Feb 23, 2019 at 13:07
  • Try: <input type="text" name="username" [(ngModel)]="name"> Commented Feb 23, 2019 at 13:08
  • 1
    Check out this:stackblitz.com/edit/angular-ga6rry Commented Feb 23, 2019 at 13:12

3 Answers 3

5

Set default value to "" for name property and use [(ngModel)]="name" for input type

HTML Code:

  <mat-form-field class="example-full-width">
    <input matInput placeholder="Enter name" [(ngModel)]="name" >
  </mat-form-field>

  <button (click)="lgbtnclick()" type="button" name="button">Test</button>

TS Code:

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

@Component({
  selector: 'input-overview-example',
  styleUrls: ['input-overview-example.css'],
  templateUrl: 'input-overview-example.html',
})
export class InputOverviewExample {
  name: string="";

  constructor() { }

  ngOnInit() {
  }

  lgbtnclick() {
    alert(this.name)
    console.log(Error)
  }
}

Stackblitz

Sign up to request clarification or add additional context in comments.

14 Comments

Its showing console log ƒ Error() { [native code] }
Remove this:console.log(Error)
Without typing anything its always return empty string
I'm using your code what you enter in dev live server. that same code in my page
Just forked it and share the link
|
1

Angulars data binding differs from jQuery or plain JavaScript and won't work using the "name" attribute of the input element. Please refer to either Reactive Forms or Template driven Forms.

In your example, the template driven form could be applied. Angular works by binding the value of an input to that of an instance variable of the hosting class with [(ngModel)]="name". Though it is discouraged to do it, the [ and ( refer to a property AND event binding (two-way data binding). Find out more about it here.

To use the ngModel directive, you need to include the FormsModule in your respective module.

The HTML code:

<input matInput placeholder="Enter name" [(ngModel)]="name" >
<button (click)="lgbtnclick()" type="button" name="button"></button>

The Typescript code:

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

@Component({
  selector: 'app-about',
  styleUrls: ['about.component.css'],
  templateUrl: 'about.component.html',
})
export class AboutComponent {
  name: string="";

  constructor() { }

  lgbtnclick() {
    alert(this.name)
    console.log(Error)
  }
}

4 Comments

This is also showing like this ƒ Error() { [native code] }
Please remove this line console.log(Error) and post the complete console output if there are any errors.
Can I know why its showing like this issue ** ƒ Error() { [native code] }** ?
Because Error in console.log(Error) apparently evaluates ** ƒ Error() { [native code] }**
1

Hi the name attribute in your html code doesn't actually bind to your typescript file. It's just an attribute on the HTMLElement. If you want that value there are actually a lot of options. The simplest solution here is just attach the name variable to the value attribute of the input or good'ol ngModel (make sure to import ReactiveFormsModule and FormsModule in your AppModule)

Value Solution

html

<input type="text" name="username" [value]="name">
<button (click)="lgbtnclick()" type="button" name="button"></button>

ts

import { Component, OnInit } from "@angular/core";
@Component({
  selector: "app-about",
  templateUrl: "./about.component.html",
  styleUrls: ["./about.component.scss"]
})
export class AboutComponent implements OnInit {
  name: string;
  constructor() {}

  ngOnInit() {}

  lgbtnclick() {
    alert(this.name);
    console.log(Error);
  }
}

ngModel Solution

html

<input type="text" name="username" [(ngModel)]="name">
<button (click)="lgbtnclick()" type="button" name="button"></button>

ts

import { Component, OnInit } from "@angular/core";
@Component({
  selector: "app-about",
  templateUrl: "./about.component.html",
  styleUrls: ["./about.component.scss"]
})
export class AboutComponent implements OnInit {
  name: string;
  constructor() {}

  ngOnInit() {}

  lgbtnclick() {
    alert(this.name);
    console.log(Error);
  }
}

4 Comments

Now also display empty
what method did you use? ngModel or the value?
I'm not using method only this two lines in html file.
Guys still i'm facing same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.