Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear/Reset Material AngualrJS <md-autocomplete>

I want to reset <md-autocomplete> control in Material AngularJS.

https://material.angularjs.org/latest/#/demo/material.components.autocomplete
https://material.angularjs.org/latest/#/api/material.components.autocomplete/directive/mdAutocomplete

Basically I want to reset it to it's initial state. I can access ng-model & assign null to it. But, it doesn't remove the displayed text contained in md-item-text attribute.

Can please someone let me know how can I solve the same.

like image 523
user2538076 Avatar asked May 26 '15 12:05

user2538076


2 Answers

You need to clear the search text, have a look at this codepen: http://codepen.io/anon/pen/QbGZWP?editors=101

I created a button that calls the clear function:

function clear() {
  self.selectedItem = null;
  self.searchText = "";
}

These are the attributes set on the md-autocomplete directive:

<md-autocomplete 
  md-selected-item="ctrl.selectedItem" 
  md-search-text="ctrl.searchText" 
  md-items="item in ctrl.querySearch(ctrl.searchText)" 
>

Note: You might need the other attributes too, depends on your case.

like image 93
Belicosus Avatar answered Sep 28 '22 15:09

Belicosus


you may use md-selected-item-change to handle this. for example

    <md-autocomplete flex
       md-item-text="item.Text"
       md-items="item in data"
       md-search-text-change="query(searchText)"
       md-search-text="searchText"
       md-selected-item="selectedItem"
       md-no-cache="false"
       md-input-minLength="2"
       md-input-name="exampleAutocomplete"
       md-selected-item-change="addSelectedItem();"
       md-floating-label="Nereye">
           <md-item-template>
               <span md-highlight-text="searchText">{{item.Text}}</span>
           </md-item-template>
           <md-not-found>No matching were found were "{{searchText}}".</md-not-found>
    </md-autocomplete>

and your controller side, you must define a function with name "addSelectedItem" and assign searchText with an empty string. like;

$scope.addSelectedItem = function () {
            $scope.searchText = ''; 
};

it works for me. hopefully it will help you to solve your problem.

like image 24
Vecihi Baltacı Avatar answered Sep 28 '22 16:09

Vecihi Baltacı