如何根据另一个字段的值显示/隐藏一个字段
Angular 2 Dynamic Forms How to show/hide a field based on the values of another
环境:Angular 2 RC 4, Typescript.
我有一个动态Angular表单的实现。我总共有4个字段-工作,电子邮件,问题和答案
问题:
dob: new TextboxQuestion({
key: 'dob',
label: 'Date of Birth',
value: '',
}),
email: new TextboxQuestion({
key: 'email',
label: 'Email Address',
value: '',
type: 'email',
}),
securityQuestion: new TextboxQuestion({
key: 'challengeQuestion',
label: 'Security Question',
value: '',
disabled: true
}),
securityAnswer: new TextboxQuestion({
key: 'challengeAnswer',
label: 'Write your answer',
value: '',
})
主/父组件的HTML片段
<dynamic-form (responsesEvt)="onFormSubmitResponseReceived($event);" [sections]="sections" class="col-md-8"></dynamic-form>
动态表单组件<form (ngSubmit)="onSubmit()" [formGroup]="form">
<div *ngFor="let question of questions" class="form-row" >
<df-question [question]="question" [form]="form" *ngIf="!section.hidden"></df-question>
</div>
<div class="row">
<div class="col-md-5"></div>
<div class="col-md-7 btn-row">
<button type="submit" class="btn btn-primary pull-right" [disabled]="!form.valid">I agree with the Terms & Conditions</button>
<!--<button type="submit" class="btn btn-primary pull-right" [disabled]="!form.valid || subbmittedalready">I agree with the Terms & Conditions</button>-->
<button type="button" class="btn btn-default pull-right">Cancel</button>
</div>
</div>
</form>
df-question组件
<div [formGroup]="form">
<div class="col-md-5"><label class="pull-right" [attr.for]="question.key"><span class="errorMessage">* </span>{{question.label}}</label></div>
<div class="col-md-7" [ngSwitch]="question.controlType">
<input *ngSwitchCase="'textbox'" [formControlName]="question.key" [id]="question.key" [type]="question.type" class="form-control"
[placeholder]="question.placeHolder"
/>
</div>
<div class="col-md-5"></div>
</div>
这是我的工作流程
Step 1 Initially at form load, only dob and email show up.
Step 2 User types in the dob and email. DOB and email are sent to the backend and if the info is correct, a "question" is returned.
Step 3 Now the question and answer fields appear (assuming that above step was successful). The question field is populated with the value returned from the server through the previous request
Step 4 User submits the form with dob, email, question and answer
关于如何使用动态表单来实现这一点,有什么想法吗?
我想可以以某种方式在工作后抛出一个事件,电子邮件被填满,调用后端并获得问题。使用一个变量来隐藏/显示基于上述响应的成功/失败的问题和答案?
如果这是一个好方法,我怎么能感觉到用户已经填写了前两个字段并进行后端调用?在收到服务器响应后,如何更新问题字段(特别是使用动态表单)?
如果没有,有没有更简单的方法?
我知道这是一个迟来的答案,但我认为本质上这个苏格兰威士忌。IO post涵盖了你需要做的基本内容。
您创建一个表单组,并根据字段的值交换验证,并且在视图中,您隐藏了使用ngif禁用的字段
相关文章:
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 带有验证和隐藏字段值的提交按钮
- 主干窗体隐藏字段未呈现
- 清除以前的$_GET值或不获取仅隐藏字段的值
- 添加和删除隐藏字段数组中的值,而不提交表单
- 在控制器上使用“$watch”时,为什么不更新此隐藏字段
- 将选项值附加到隐藏字段
- JQuery获取隐藏字段的值
- HTML提交表单,同时包含空字段检查和按钮隐藏
- MVC+访问控制器中的隐藏字段
- 如何隐藏&使用jQuery禁用依赖于另一个字段值的字段
- ASP.net Javascript函数中的隐藏字段为null
- 在第一页加载时隐藏字段,而不是在php发布之后
- 使用JavaScript获取隐藏的Django字段
- 提交表单时,显示或识别隐藏的必填字段并将其集中
- 表单使用js、html,而不是隐藏字段
- HTML5要求隐藏文件字段-反馈位置
- 当javascript中的强制字段隐藏在条件中时,如何限制它们的验证
- 为隐藏字段隐藏jquery验证器消息
- 如何使一些字段隐藏取决于其他字段的值AngularJS, Razor, c#