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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With