angular - 我没有在 typescript angular7 中获取文本框值

标签 angular typescript scripting

我正在使用 angular7。只需制作一些文本框,然后提醒该文本框值。但我无法获得 typescript 中的值。 请帮助我如何继续这一步。

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)
   }
}

警告信息:

undefined

最佳答案

name 属性的默认值设置为 "" 并使用 [(ngModel)]="name" 作为输入类型

HTML代码:

  <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代码:

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

关于angular - 我没有在 typescript angular7 中获取文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54841928/

相关文章:

javascript - Typescript 和 Uncaught SyntaxError : Block-scoped declarations (let, const, function, class) 在严格模式之外还不支持

javascript - Angular(Javascript)如何控制mousemove事件的速度?

html - 如何使用 Angular 2 中的按钮触发 ngModelChange

asp.net - Google 身份验证器集成

c - 卢阿 :new from C API

bash - 通过树莓派上的 Chrome 标签旋转?

ruby - 发出一个命令并运行多个 Ruby 文件

javascript - Angular 2 DOM 绑定(bind)数据不准确

angular - @Types/node/index.d.ts TypeScript 错误 TS2309

javascript - 将嵌套 JSON 附加或添加到另一个 JSON 对象中