如何通过 Angular 2 中的类名获取 Inputs 的值 [英] How to get values of Inputs through class name in angular 2

查看:25
本文介绍了如何通过 Angular 2 中的类名获取 Inputs 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组输入元素:

    <input type="text" name="firstName" class="mandate" />
    <input type="text" name="MiddleName" class="mandate" />
    <input type="text" name="lastName" class="mandate" />
    <input type="text" name="Address" class="mandate" />'

在Jquery中,很容易通过Class获取值,或者我们可以很容易地在所有输入元素都有的类的基础上执行一些常见的事件,如

In Jquery, it is very easy to get values through Class or we can easily perform some common event on the basis of class that all input elements have like

$('.mandate').each(function(){
console.log("Some....")
});

但我在徘徊如何在 Angular 2 中完成它.我是 Angular 2 的新手.

But I am wandering How it will be done in Angular 2.I am a completely newbie in Angular 2.

推荐答案

你可以在 angular 中使用元素引用 (ElementRef) 和在 jQuery 中使用的 querySelector.

you can use element reference (ElementRef) in angular and querySelector the way you used in jQuery.

  constructor(private elementRef: ElementRef) {
  }

  // this is inside any of the method
  // this is to select multiple 
  this.elementRef.nativeElement.querySelectorAll('.mandate');

  // this is to select single
  this.elementRef.nativeElement.querySelector('.mandate')

这篇关于如何通过 Angular 2 中的类名获取 Inputs 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆