2

I have a table's headers which has to be populated based on the json {id, name} this.id is the ngModel name I have used in the component which need to be mapped for custom filter respectively.

say,

 list=[{id:ageSearch,name:age},{id:adressSearch,name:adress}]

so when I enter the value for the first input text field,ageSearch is to be populated and it has to filter the data,similarly for adressSearch now I would like to know how to map the variable ageSearch,adressSearch dynamically inside a ngfor.

I tried using

 <div *ngFor="let item of list;let index = index;trackBy:trackByIndex;">
      <input [(ngModel)]="item[id]" placeholder="item">
    </div>

But , its populating the value ageSearch instead of binding the variable . Can anyone please say whether I am trying something which is feasible or not. If so how can I achieve the expected flow.

The expected result should be

<div>
     <input [(ngModel)]="ageSearch" placeholder="item">
</div>
<div>
     <input [(ngModel)]="adressSearch" placeholder="item">
</div>

Edit: in component I have variables declared as let ageSearch:any; let adressSearch:any;

these values are to be passed to the custom filter. so I ahve to set the ngModel of the inputs to bind these variable names so that initially they appear blank on load and when we enter values they reflect the value of ageSearch and adressSearch resp. which is then passed to the custom filter. Thank You.

2 Answers 2

3

After some hit and trial ,I got this working by replacing the

<input [(ngModel)]="item.id" name="{{item.name}}" placeholder="item">

with

<input [(ngModel)]="this[item.id]" name="{{item.name}}" placeholder="item">

Thank you.

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

Comments

0

Change it to this:

<input [(value)]="item.id" placeholder="item">

or to this:

<input [(value)]="item['id']" placeholder="item">

If you do need to use ngModel as in your example, then you will need to give it a name as well:

<input [(ngModel)]="item.id" name="{{item.name}}" placeholder="item">

1 Comment

thanks dexter but it didnt work , I have edited the explanation of the flow,I want to map the variable reference to the input not the value.

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.