<div>
<input #ipt type="text"/>
</div>
Run Code Online (Sandbox Code Playgroud)
是否可以从组件类访问模板访问变量?
即,我可以在这里访问它,
class XComponent{
somefunction(){
//Can I access #ipt here?
}
}
Run Code Online (Sandbox Code Playgroud) 我的 ngAfterViewInit 函数未按预期工作。我感觉我在这里错过了一些东西。
ngOnInit() {
this.dataService.getUsers().subscribe((users) => {this.users = users) ;
}
ngAfterViewInit() {
if (document.getElementById('target')) {
document.getElementById('target').scrollIntoView({
behavior: 'auto',
block: 'center',
inline: 'center',
});
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,在视图完成加载后,我使用ngAfterViewInint()函数滚动到当前项目。在里面ngOnInit(),如果我不打电话subscribe(),只是在那里放一个变量。它工作得很好。但我所问的情况并非如此。
setTimeOut()我通过使用inside解决了这个问题ngAfterViewInit()。但我不喜欢它。我不喜欢它依赖于超时这一事实。我认为会有更好的解决方案。有理想吗?谢谢大家。
我想在单击按钮时打印文本框的值。但它抛出空指针异常。我还需要在文本框中保留一些值,我不明白为什么?。无论我在文本框中输入什么,当我点击按钮时,我都需要打印文本框的值有什么问题?
下面是我的代码:
.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);
}
}
Run Code Online (Sandbox Code Playgroud)
HTML文件
<br>Welcome.<br>
Place - <input type="text" value="Sydney" ng-model="placeId" />
<button (click)="myFunc(placeId)" formtarget="_blank">Test</button>
Run Code Online (Sandbox Code Playgroud)
错误:
ERROR TypeError: Cannot read property 'value' of null
Run Code Online (Sandbox Code Playgroud)