Search code examples
javascriptjqueryangularjsangular-filters

How to Use DropDown Selected Vaue as Parameter for Filter in AngularJs?


Iam Using Select2 Drop down which is not Filter on Select :-

My Code :-

<div class="form-group">
                                    <script type="text/javascript">
                                            jQuery(document).ready(function($)
                                            {
                                                $("#select-res").select2();
                                        </script>
                                    <select class="form-control select" ng-model="displayName"  id="select-res">
                                        <option data-ng-repeat="objin objs" value="{{obj.displayName}}">{{obj.displayName}}</option>
                                    </select>
                                    <script>
                                        $('#select-res').select2().on('change', function(e) {
                                          var selectedval= $(this).val();
                                           alert(selectedval);
                                           // $("#filterselected").html(selectedval);
                                           // $("#filterselected").val(selectedval+' ');
                                            document.getElementById("filterselected").value = selectedval;
                                        });
                                    </script>
                                      <input id="filterselected" type="text" ng-model="displayName" />
                                </div>

I used data-ng-model="displayName" for Two Way Binding

Filter Code :-

<div data-ng-repeat="obj in objs | filter:displayname" >
       Results ....
</div>

The Options i Selected in Select2 Drop Down Is Not Being Input For Filter Parameter .

I have written the Selected Value in to a Text Box With data binding as ng-model ="displayName" but Not Filtering :-(


Solution

  • Rather than using select2 library with you should use it angular version which ui-select2

    Which provides better binding while dealing with angular scope. As you want to bind change event here you could simply use ng-change angular native directive.

    Markup

    <select ui-select2 ng-model="number" data-placeholder="Pick a number" ng-change="changeSelect()">
        <option value=""></option>
        <option value="one">First</option>
        <option value="two">Second</option>
        <option value="three">Third</option>
    </select>