我对 Angular2 的了解有限。通过使用jquery,我们可以轻松获取属性值。 例如 在 HTML 中
<input id="foo" type="text" name="foo">
在jquery中
$(document).ready(
function ()
{
console.log($('#foo').attr('type'));// output foo
});
在 Angular2 中,如果我们使用响应式(Reactive)形式,我们会这样编写输入字段:
<input formControlName="name" id="foo" type="text" name="foo">
我的要求是在组件中动态获取属性(名称)的值。
最佳答案
你可以做类似的事情
模板:
<input #myinput formControlName="name" id="foo" type="text" name="foo">
类(class):
@ViewChild('myinput ') input: ElementRef;
ngAfterViewInit() {
console.log(this.input.nativeElement.getAttribute('type'));
}
关于jquery - 如何从Angular2中的FormControl对象获取属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48277859/