0

I am new in Angular. I want to show/hide the file div using drop down (Select) menu.If I choose "custom", it will show File element. But I choose "none", it will hide as well as remove the file (if any) element. I am unable to show the hidden file element. Please provide some feedback?

<div class="form-group">
  <label for="credentials">Credentials</label><br />
  <select name="select_file" class="form-control" required ng-model="myVar">
    <option value="">Choose a option</option>
    <option value="false">none</option>
    <option value="true">custom</option>
  </select>
  <div *ngIf="myVar">
    <input
      type="file"
      class="form-control"
      id="credentials"
      accept=".json"
      [(ngModel)]="userdata.credentials"
      (change)="handleFileInput($event.target.files)"
      name="credentials"
    />
  </div>
</div>

2 Answers 2

1

First of all it's not a Angular JS, it's looks like Angular 2+. About your problem:

[value]="..." only supports string values
[ngValue]="..." supports any type, including boolean

e.g.

<h1>My Application</h1>
<select [(ngModel)]="myVar">
  <option [ngValue]="''">Choose a option</option>
  <option [ngValue]="false">none</option>
  <option [ngValue]="true">custom</option>
</select>

Also try to use Reactive Forms instead of ngModel

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

Comments

0
    <div class="form-group">
  <label for="credentials">Credentials</label><br/>
  <select name="select_file" class="form-control" required [(ngModel)]="myVar">
    <option value="">Choose a option</option>
    <option value="false">none</option>
    <option value="true">custom</option>
</select>
<div *ngIf="myVar === true">
    <input 
    type="file" 
    class="form-control" 
    id="credentials" 
    accept=".json"
    [(ngModel)]="userdata.credentials"
    (change)="handleFileInput($event.target.files)"
    name="credentials" 
    >
  </div>

You need to use ngModel . In Ts file, just define myVar variable.Please try this code.

4 Comments

Your answer is correct. But value is replaced by [ngValue] as it supports any type, including boolean.
@YatishBathla You are right, value is always a string. But in our case, I guess it will not cause any issue. Because in div we have if condition of myVar === true .So it will not allow empty string also. Only value true will get allowed
I have tried with value but then file div is not visible when i clicked on custom option.
Oh okay, sorry my mistake. I forgot to do a quote. Try this *ngIf=" myVar === 'true' " .

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.