[英]How do I get the value from my Form control to use in Javascript in Angular2
Here is my Form Control: 这是我的表单控件:
<div class="field">
<label for="AgeInput">Age</label>
<input type="text"
id="AgeInput"
[formControl]="agefld">
<div *ngIf="agefld.hasError('required')"
class="ui error message">Age is required</div>
<div *ngIf="agefld.hasError('invalidAge')"
class="ui error message">Age must be greater than 0</div>
</div>
<div class="field">
<label for="dobInput">Date of Birth</label>
<input type="text"
id="dobInput"
[formControl]="dob">
<div *ngIf="dob.hasError('required')"
class="ui error message">Date of Birth is required</div>
<div *ngIf="dob.hasError('invalidDate')"
class="ui error message">Age and date do not compute</div>
</div>
Here is my custom function : 这是我的自定义函数:
function validateDob(control: FormControl): {[s:string]: boolean}{
var today = new Date();
var inDate = new Date(control.value);
var calcAge = Math.abs(today - inDate);
var ageDif = Math.floor(calcAge / (1000 * 60 * 60 * 24 * 365))
var otherAge = 51;
alert(" Entered age : " + parseInt(getValue("agefld"));
if (50 != ageDif){
return {invalidDate: true}
}
}
What I would like to do is calculate if the entered date is valid based on the entered agefld. 我想根据输入的agefld计算输入的日期是否有效。 But I keep getting "Agefld" is undefined.
但我一直在获取“ Agefld”未定义。 I have tried several different things such as {{agefld}}, [{agefld}] and even this.agefld.
我尝试了几种不同的方法,例如{{agefld}},[{agefld}]甚至this.agefld。 but same message keeps appearing.
但相同的消息不断出现。 This cannot be that difficult.
这不会那么困难。
You have to create a directive that implements the Validator interface. 您必须创建一个实现Validator接口的指令。 And use this directive in HTML form.
并以HTML格式使用此指令。 Don't forget to set this directive in the ngModule declarations tag.
不要忘记在ngModule声明标签中设置此指令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.