【发布时间】:2018-07-13 09:51:03
【问题描述】:
我想在单击按钮时打印文本框的值。但它会抛出空指针异常。我还需要在文本框中保留一些值,我不明白为什么?无论我在文本框中输入什么,当我点击按钮时,我都需要打印文本框的值有什么问题?
下面是我的代码:
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);
}
}
HTML 文件
<br>Welcome.<br>
Place - <input type="text" value="Sydney" ng-model="placeId" />
<button (click)="myFunc(placeId)" formtarget="_blank">Test</button>
错误:
ERROR TypeError: Cannot read property 'value' of null
【问题讨论】:
-
通常不鼓励在 Angular 应用程序中直接操作 dom。我会使用模板变量来引用输入并将值直接传递给 myFunc。
标签: javascript html angular