How can I get value of input tag type hidden to ngModel?

I tried to get value of input tag but I always receive name of asset. Anyone can help me? Thanks very much.

<select class="form-control" [(ngModel)]=id name="assetID">
    <option *ngFor="let asset of arrAssets" selected>
        <input type="hidden" value="{{ asset.Id }}" name="id">
        {{ asset.Name }}
    </option>
</select>

Upvotes: 0

Views: 534

Answers (1)

Falyoun
Falyoun

Reputation: 3976

You can do it by using (input) on your <input> element like:

<select class="form-control" [(ngModel)]=id name="assetID">
     <option *ngFor="let asset of arrAssets; let i = index;" selected>
             <input type="hidden" (input)="inputChanged($event , i)" value="{{ asset.Id }}" name="id">
             {{ asset.Name }}
      </option>
</select>

I have add the i as index in case you want to detect each input on its own

Moving to .ts file

export class Home {

  constructor(){}

  inputChanged(eve, i) {

   console.log(eve.target.value); // This should print the value of input element

   console.log(i); // This should print the index of input element which has changed
  }

}

Upvotes: 1

Related Questions