Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度:点击按钮获取文本框的值_Javascript_Html_Angular - Fatal编程技术网

Javascript 角度:点击按钮获取文本框的值

Javascript 角度:点击按钮获取文本框的值,javascript,html,angular,Javascript,Html,Angular,我想在单击按钮时打印textbox的值。但它抛出nullpointerexception。我还需要在文本框中保留一些值,我不明白为什么?。无论我在文本框中键入什么,当我点击按钮时,我需要打印文本框的值。问题是什么 下面是我的代码: ts文件 import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-mypage', templateUrl: './mypage.component.htm

我想在单击按钮时打印textbox的值。但它抛出nullpointerexception。我还需要在文本框中保留一些值,我不明白为什么?。无论我在文本框中键入什么,当我点击按钮时,我需要打印文本框的值。问题是什么

下面是我的代码:

ts文件

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

@Component({
  selector: 'app-mypage',
  templateUrl: './mypage.component.html',
  styleUrls: ['./mypage.component.scss']
})
export class mypageComponent implements OnInit {

  constructor(private router: Router) {

  }

  ngOnInit() {
  }



  myFunc() {

      var num1 = ((document.getElementById("exchageRateDate") as HTMLInputElement).value);
      console.log(num1);
  }

}
<br>Welcome.<br>
Place - <input type="text" value="Sydney" ng-model="placeId" />
<button (click)="myFunc(placeId)" formtarget="_blank">Test</button>
HTML文件

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

@Component({
  selector: 'app-mypage',
  templateUrl: './mypage.component.html',
  styleUrls: ['./mypage.component.scss']
})
export class mypageComponent implements OnInit {

  constructor(private router: Router) {

  }

  ngOnInit() {
  }



  myFunc() {

      var num1 = ((document.getElementById("exchageRateDate") as HTMLInputElement).value);
      console.log(num1);
  }

}
<br>Welcome.<br>
Place - <input type="text" value="Sydney" ng-model="placeId" />
<button (click)="myFunc(placeId)" formtarget="_blank">Test</button>

似乎您忘记在输入字段中添加
id

<input id="exchageRateDate" type="text" value="Sydney" ng-model="placeId" />

似乎您忘记在输入字段中添加
id

<input id="exchageRateDate" type="text" value="Sydney" ng-model="placeId" />

您需要设置输入标记移除ng模型的id,因为它是一个角度JS(1.x.x)角度(2/4/5/6/7/8)

在html中

<br>Welcome.<br>
 Place - <input id="exchageRateDate" type="text" value="Sydney"  />
 <button (click)="myFunc()" formtarget="_blank">Test</button>

下面是一个工作示例:

您需要设置输入标记移除ng模型的id,因为它是一个角度JS(1.x.x)角度(2/4/5/6/7/8)

在html中

<br>Welcome.<br>
 Place - <input id="exchageRateDate" type="text" value="Sydney"  />
 <button (click)="myFunc()" formtarget="_blank">Test</button>
下面是一个工作示例:


(document.getElementById(“Summary”)作为HTMLInputElement);

(document.getElementById(“Summary”)作为HTMLInputElement);

通常不鼓励在角度应用程序中直接操作dom。我会使用一个模板变量来引用输入并将值直接传递给myFunc。通常不建议在Angular应用程序中直接操作dom。我会使用一个模板变量来引用输入,并将值直接传递给myFunc。不鼓励只使用代码的答案,因为它们不会为将来的读者提供太多信息。请对您编写的内容提供一些解释。不鼓励只使用代码的答案,因为它们不会为将来的读者提供太多信息。请提供对您所写内容的一些解释为什么要在这里使用“document.getElementById()”,而我们可以使用一个可以保持正常的绑定。最好使用“ViewChild”。它更接近于“角度”方法。检查答案。当我们可以使用一个可以保持正常的绑定时,为什么要在这里使用“document.getElementById()”。最好使用“ViewChild”。它更接近于“角度”方法。检查答案。
<input type="text" class="textbox" id="Summary" name="Summary"placeholder="Summary" value="{{Item.summary}}">
(document.getElementById("Summary") as HTMLInputElement).value;