简体   繁体   English

获取生成的隐藏字段的值

[英]Get value of an generated hidden field

I'm using the Datepicker from angular2-materialize, it is based on Pickadate, and I need to send a different value to the server than what is displayed to the user. 我正在使用angular2-materialize中的Datepicker,它基于Pickadate,我需要向服务器发送与向用户显示的值不同的值。 Classical case. 经典案例。 For this, Pickadate provides an option : formatSubmit which creates an hidden input which holds that internal value. 为此,Pickadate提供了一个选项:formatSubmit,它创建一个保存该内部值的隐藏输入。 I'm even able to have this input having the same name that my main input (with hiddenName parameter). 我甚至可以让这个输入与我的主输入(使用hiddenName参数)具有相同的名称。

<input type="date" id="dateOfEvent" name="dateOfEvent" 
       [(ngModel)]="claimFile.dateOfEvent" class="datepicker" 
       materialize="pickadate" [materializeParams]="[{format: 'd mmmm yyyy', formatSubmit: 'yyyy-mm-dd', hiddenName: true}]" required />

That generates an hidden input below the component : 这会在组件下方生成隐藏的输入:

<input type="hidden" name="dateOfEvent" value="2016-10-20">

The problem is that hidden input is not binded to my ng2 model. 问题是隐藏的输入没有绑定到我的ng2模型。 How can I do that ? 我怎样才能做到这一点 ? If it isn't possible, how can I get a way to access the value of my hidden input ? 如果不可能,我怎样才能获得访问隐藏输入值的方法? thx a lot 多谢

<input type="hidden" #hidden name="dateOfEvent" value="2016-10-20">
@ViewChild('hidden') hidden:ElementRef;

ngAfterViewInit() {
  console.log(this.hidden.nativeElement.value);
}

If it is not possible to add a template variable to the hidden element 如果无法将模板变量添加到隐藏元素

consturctor(private elRef:ElementRef){}

ngAfterViewInit() {
  console.log(this.elRef.querySelector('input[type="hidden"]').value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM