angularjsangularjs-ng-changeangular-chosen

AngularJS - Chosen localytics ng-change not working


I'm trying to detect when I select an option from an AngularJS chosen localytics dropdown.

Here's the code for the dropdown:

<select chosen data-placeholder="Buscar..." 
    class="w-100" 
    ng-model = "medSearchType"
    ng-change = "changeMedSearch(medSearchType)">
        <option value="Medicamento">Medicamento</option>
        <option value="Componente">Componentes Activos</option>
</select>

And in my controller I have the following function:

$scope.changeMedSearch = function (searchType) {
    console.log(searchType);
}

It works fine the first time, but if I want to select another option in the console I get the following error, and alos the dropdown won't select the other option I want to select:

Uncaught TypeError: Cannot set property 'selected' of undefined

How can I solve this?

Thanks a lot.


Solution

  • I followed MrJSingh's advice and use ng-options, but the problem still happened.

    I added then an empty and it finally worked

    <select chosen data-placeholder="Buscar..." 
         ng-model = "medSearch"
         ng-change = "changeMedSearch(medSearch)"
         ng-options = "size as size for size in medSearchTypes">
         <option></option>
    </select>