5

I have the following code:

HTML:

<select>
  <option *ngFor="let item of items">{{item}}</option>
</select>

Some Angular service:

items = [
{name: "item1", type: "type1"},
{name: "item2", type: "type2"},
{name: "item3", type: "type2"}

];

And some filtering function, which filters an array by type and returns new array.

I haven't got problems with filtering by button like:

<button type="button" (click)="Filter('type2')"></button>

But I can't do it using dropdown.

UPD: Incorrect explanation. Here live demo. Need to filter array with (change) event at select tag using brandName

1
  • 1
    Will you be little more specific about the issue? Commented Oct 4, 2018 at 11:31

2 Answers 2

5

Set ngModel to get selected value and change event to update the list:

<select [(ngModel)]="selectedBrand" (change)="valueSelected()">
  <option *ngFor="let item of brandName">{{ item }}</option>
</select>

Then filtering items in the component when value has been changed:

public selectedBrand;
public valueSelected() {
    this.goods = this.goodsService.goods.filter(item => item.name === this.selectedBrand);
}

Demo

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

3 Comments

Thanks, but I need to filter another array. Incorrectly expressed my thoughts
Thanks a lot. It was easier than I thought.
@Shiv, I updated the link to the demo. It was added by someone else and was an exact copy to the demo in the question. I added copied over code from my answer it seems to work.
0

Try this

AppComponent.ts file

export class AppComponent{

    constructor(){}
    items = [
        {name: "item1", type: "type1"},
        {name: "item2", type: "type2"},
        {name: "item3", type: "type2"}
    ];

makeArr(e){
    console.log(e);
}

}

html

<select (change)='makeArr($event.target.value)'>
    <option *ngFor="let item of items">{{item.name}}</option>
</select>

you are having the problem with displaying the value from the object. retrieve the value of the object using . or you can use the []

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.