0

I have a dropdown like

<select [(ngModel)]="car.Marketid" required name="Marketid" id="Marketid" 
 ngModel class="form-control">
    <option [ngValue]="select" disabled [selected]="true">--Select Market--
    </option>
    <option *ngFor="let code of names" value="{{code.marketId}}">
    {{code.marketName}} - {{code.SpecMarketCode}}</option>
</select>

In my "save" method in .ts file I need to get Marketid which is selected from this drop-down and MarketName but here [(ngModel)]="car.Marketid" so I'm getting the only id in my save method in ts file. I need to get marketname also. How can I get?

update: in my save methode i tried to filter but i am getting Illegal return statement" error in find/filter methode
name => name.marketId this part is compleatly undefined and getting illegal error

    save(car: marketmodel) {
    this.submitted = true
    this.marketService.SaveMarketData(car)
        .subscribe(response => 
            const name = this.name.filter(name => name.MMId === 
  car.marketId);//  error here-name => name.marketId this part is compleatly 
                //undefined and getting Illegal return statement error
            this.getmarketdetails();

        });
}
baby
  • 11
  • 2

1 Answers1

0

Without changing the view you can lookup the code inside the names array as follows with the selected Car.MarketId model:

save() {
  const name = this.names.find(name => name.marketId === this.car.MarketId);

  this.aService.doSomething(name.marketId, name.marketName);
}
Dieterg
  • 16,118
  • 3
  • 30
  • 49
  • hi, i used this one also. but am getting "Illegal return statement" error in find/filter methode – baby Feb 14 '18 at 10:36
  • name => name.marketId this part is compleatly undefined and getting illegal error – baby Feb 14 '18 at 10:47
  • It's hard to say without knowing the exact contents of your `names` array. Can you reproduce a small example on stackblitz? – Dieterg Feb 14 '18 at 10:52
  • this.names contains: 0 : SpecMarketCode : 211 marketId : 1 marketName : "NetherLands" __proto__ : Object 1 : SpecMarketCode : 232 marketId : 2 marketName : "Switzerland" – baby Feb 14 '18 at 11:11
  • getting "Illegal return statement" error in name.marketId – baby Feb 14 '18 at 11:49
  • Double check your syntax. Make sure your open/closing brackets match `{` `}` – Dieterg Feb 14 '18 at 12:08
  • save(car: marketmodel) { this.submitted = true this.marketService.SaveMarketData(car) .subscribe(response => const name = this.name.filter(name => name.MMId === car.marketId);//error here this.getmarketdetails(); }); } – baby Feb 14 '18 at 12:29
  • this is inside a subscribe methode – baby Feb 14 '18 at 12:29