0

I am currently building some functionality in an Angular2 component where an array of objects is looped through to create the options for a form select box.

I have this working and when the select box option is changed I call a handler function which I intend to pass the chosen option through to.

The problem I am having at the moment is that I cannot get the object to pass through from the view to the controller. The object is passed though as a string:

[object object]

Here is my code:

view:

        <div class='searchbar-wrapper'>
            <h5>MANUFACTURER</h5>
            <select class='manufacturers' #m [(ngModel)]="selectedManufacturer" (change)="changeManufacturer(m.value)">
                <option>-- please select --</option>
                <option *ngFor="let manufacturer of manufacturers" [ngValue]="manufacturer">{{manufacturer.name}}</option>
            </select>
        </div>

controller:

changeManufacturer(manufacturer) {
    this.productsLoaded = false;
    this.selectedManufacturer = manufacturer;
    this.productPayload.manufacturer = manufacturer.id;
    this.getProducts();
}

As you can see, I am trying to pass the object through to the controller but this isn't working. Can anyone suggest as to why it is getting passed through as as string?

Thankss

2
  • I would suggest you to use manufacturer.id as ngValue and then you can use that id to set the correct SelectedManugacturer from manfacturers list/Array in your class. It would be optimal. Commented Sep 16, 2016 at 12:23
  • Thanks for your reply. I have tried that but I still get this back as a string: "13: 13" Commented Sep 16, 2016 at 12:43

1 Answer 1

1

I managed to get this working my changing the following line from:

<select class='manufacturers' #m [(ngModel)]="selectedManufacturer" (change)="changeManufacturer(m.value)">

to:

<select class='manufacturers' [(ngModel)]="selectedManufacturer" (ngModelChange)="changeManufacturer($event)">
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.