How to set an option with a specific title attribute as the selected item in Angular using Bootstrap's selectpicker?

70 views Asked by At

Some of the options may have the same value, so I can not select the right option by setValue(...) so I was tring use the 'selected' but it didn't work.

I tried this, but it didn't work.

$('#my-select').find('option[title="2"]').prop('selected', true);
$('.selectpicker').selectpicker('refresh');
1

There are 1 answers

3
Eliseo On

In Angular you should avoid use jQuery to change a value. You should use two-binding way. Angular is based to relationate variables in .ts with values in the .html (Believe me, it's the most important concept you should to have in mind)

In this case is so easy to use [(ngModel)]

<select [(ngModel)]="variable" ...>
    ...
</select>

And in .ts

variable="2"