我在Angular4中有一些经验,但我只是继承了一段使用FormControls的代码,我不知道如何使用它们.我正在设置一个注释textarea,如果isRegulatoryAuditRequired的值等于false,则需要该注释.我已经获得了必填字段,但是如果value为false并且提交了表单,我需要显示消息"需要评论".这是我的HTML,有两个单选按钮,一个textarea和一条消息标签:
<div class="btnWrapper">
<div class="leftBtn">
<span class="col-xs-12 no-padding">
<label>
<input type="radio" id="radio1" class="app-input-radio" name="isRegulatoryAuditRequired" [value]="true"
[checked]="isRegulatoryAuditRequired==true" formControlName="isRegulatoryAuditRequired" />
<span class="app-input-radio pull-left"></span>
<span class="plx5 pull-left radioText ">Yes</span>
</label>
</span>
</div>
<br />
<div class="rightBtn">
<span class="col-xs-12 no-padding">
<label>
<input type="radio" id="radio2" class="app-input-radio" name="isRegulatoryAuditRequired" [value]="false"
[checked]="isRegulatoryAuditRequired==false" formControlName="isRegulatoryAuditRequired" />
<span class="app-input-radio pull-left"></span>
<span class="plx5 pull-left radioText ">No</span>
</label>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="row mbx20">
<div class="col-sm-4">
<div>
<label>Why is the regulatory audit being performed or provide additional comment?</label>
<div>
<textarea id="comments" name="Text1" [required]="isRegulatoryAuditRequired==false" cols="100" rows="2" formControlName="comments"></textarea>
<label *ngIf="commentsRequired('comments')" style="color:red;font-weight:bold">Comments required</label>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要评估'isRegulatoryAuditRequired'和'comments'FormControls的值,看看我是否应该显示'需要评论'消息.这是我正在尝试使用的方法,但它不起作用:
commentsRequired(controlName: string) {
const control = (<FormControl>this.rotationForm.controls[controlName]);
const isRegulatoryAuditRequired = (<FormControl>this.rotationForm.controls['isRegulatoryAuditRequired']);
if (isRegulatoryAuditRequired.value === false && control.value === '' && this.formSubmitAttempt) {
return true;
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
'isRegulatoryAuditRequired.value'和'control.value'的值作为null打印到控制台,因此不显示消息.如何检索这些值?任何帮助将不胜感激.
归档时间: |
|
查看次数: |
25000 次 |
最近记录: |