I want to have md-select so that the user can clear the selected value. For instance. they choose a value, but decide that they want to clear their choice.
The behavior of md-select is to select the first record in the parameters. I would like to return it to a state in which there was no choice.
I assume that I probably need a special directive for this, so I implemented a simple directive that listens for keydown for the DELETE key.
HTML:
<div ng-controller="AppCtrl as ctrl" class="md-padding selectdemoBasicUsage" ng-cloak="" ng-app="MyApp"> <div> <h1 class="md-title">Select a state</h1> <span>I want the DELETE key to be able to clear the selected state.</span> <div layout="row"> <md-input-container> <label>State</label> <md-select ng-model="ctrl.userState" select-clear> <md-option ng-repeat="state in ctrl.states" value="{{state.abbrev}}"> {{state.abbrev}} </md-option> </md-select> </md-input-container> </div> </div> </div>
JS:
(function () { 'use strict'; angular .module('MyApp',['ngMaterial', 'ngMessages']) .controller('AppCtrl', function() { this.userState = ''; this.states = ('AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS ' + 'MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI ' + 'WY').split(' ').map(function (state) { return { abbrev: state }; }); }) .directive('selectClear', function() { return { restrict: 'A', require: 'ngModel', link : function(scope, iElement, iAttrs, ngModelCtrl) { iElement.bind('keydown', function(event) { if (event.keyCode === 46) { ngModelCtrl.$setViewValue('', event); } }) } } }); })();
Here is my code handle:
http://codepen.io/craigsh/pen/GorpVV
But this will not work - when you press the DELETE key, the first parameter value is selected.
source share