jquery - 如何从Angular2中的FormControl对象获取属性值?

标签 jquery angular angular-forms

我对 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/

相关文章:

javascript - 如何将数学随机数组输出到 HTML 中

javascript - 窗口大小jquery错误

angular - 子路由上的页面重新加载

angular - Mat-Autocomplete formControlName

javascript - 如何从子组件中获取父表单的字段?

php - 电子邮件未从表单插入 mysql 数据库

javascript - jQuery/CSS - 使元素适合屏幕(没有水平滚动条)

asynchronous - 找不到管道 'async'

javascript - Angular 形式自定义验证器消息导致错误

angular - ngIf 和 formcontrol.touched 不工作