0

I have the following use case

We present a hardcoded dropdown list to the user

Eg: Dropdown list with 4 choices say apple, orange, grape, pineapple and 'write your own'

If the user does not like those 4 choices, he/she can select the 'write your own' option from the drop down. Now the user is shown an input box where he/she can write their own fruit like say 'banana'.

How can I implement this with Angular 2 new forms module?

1
  • 1
    Please add the code that demonstrates what you're trying to accomplish. Commented Jul 22, 2016 at 5:42

1 Answer 1

3

I think this should work for you

HTML:

    <select *ngIf="!showAdditional" [(ngModel)]="mySelect" name="mySelect" (ngModelChange)="custom()">
        <option *ngFor="let o of options" [value]="o">{{o}}</option>
    </select>
    <input *ngIf="showAdditional" type="text" [(ngModel)]="additionalOption" name="additionalOptions" />

Ts:

export class SomeComponent {
    mySelect: string; 
    options: string[] = ['apple', 'orange', 'grape', 'write your own'];
    additionalOption: string;
    showAdditional: boolean = false;

    custom() {
        if (this.mySelect === 'write your own') this.showAdditional = true;
    }
}

So if the user wants to add a custom option you hide the select and show the input. You can add additional validation for angular 2 forms this is just the bare set up.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.