Angular:根据先前的选择或输入在表单上显示输入

时间:2017-09-08 20:07:26

标签: forms angular vmware-clarity

我正在使用VMware的清晰度种子回购,我正在尝试构建一个输入表单,提示其他相关信息。例如,表单具有用于身份验证类型的下拉列表选择。根据类型,我可能需要更多信息,并且该信息特定于类型。 “无”身份验证不需要更多信息。 “基本”需要用户和密码组合,而“OAuth”需要API令牌。

我尝试使用ng-switch没有运气 - 尽管有选择,但两个选项都显示了文本(我现在只使用文本,稍后会添加子表格详细信息)。

我认为我对形式字段的使用在某种程度上是错误的,但我无法弄清楚为什么以及如何。

<form>
<section class="form-block">
    <span>New Endpoint</span>
    <div class="form-group">
        <label for="endpoint.name" class="required">Name</label>
        <input type="text" id="endpoint.name" size="45">
    </div>
    <div class="form-group">
        <label for="endpoint.id">Description</label>
        <input type="text" id="endpoint.id" size="45">
    </div>
    <div class="form-group">
        <label for="endpoint.url" class="required">URL</label>
        <input type="url" id="endpoint.url" placeholder="http://endpoint.co/api" size="35">
    </div>
    <div class="form-group">
        <label for="endpoint.authType">Authentication</label>
        <div class="select" class="required">
            <select id="endpoint.authType">
                <option>None</option>
                <option>Basic</option>
                <option>OAuth</option>
            </select>
        </div>
    </div>
    <div ng-switch="endpoint.authType">
        <div ng-switch-when="Basic">
            <h1>Another form for Basic credential set</h1>
        </div>
        <div ng-switch-when="OAuth">
            <h1>Another form for OAuth token</h1>
        </div>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</section>

1 个答案:

答案 0 :(得分:3)

您可以使用*ngIf - then ;else的新语法:

<div *ngIf="endpoint.authType === 'Basic'"; then basic; else auth>
        <ng-template #basic>
            <h1>Another form for Basic credential set</h1>
        </ng-template>
        <ng-template #auth>
            <h1>Another form for OAuth token</h1>
        </ng-template>
</div>

Docs

如果您有两个以上的值,您仍然可以使用ngSwitch:

<div [ngSwitch]="conditionExpression">
   <ng-template [ngSwitchCase]="case1Exp">...</ng-template>
   <ng-template ngSwitchCase="case2LiteralString">...</ng-template>
   <ng-template ngSwitchDefault>...</ng-template>
</div>

Docs

相关问题