I'm trying to clear the input field of a filter when a user presses the esc key. As soon as I enter the mark up for the directive, the filter stops working completely.
My mark up is as follows:
<div ng-app='App'>
<div ng-controller="MyCtrl">
<input type="text" ng-model="itemSearch" clear-input clear="clearFilter()" />
<button ng-click="clearFilter()">Clear</button>
<ul>
<li ng-repeat="item in items|filter:itemSearch"> <span>{{item.value}}</span>
</li>
</ul>
</div>
</div>
And the JavaScript is as follows:
var app = angular.module('App', []);
function MyCtrl($scope) {
$scope.items = [
{value: 'one two three'},
{value: 'four five six'}
];
$scope.clearFilter = function () {
$scope.itemSearch = '';
};
}
app.directive('clearInput', function () {
function isEscapeKey(keyCode) {
if (keyCode === 27) {
return true;
}
return false;
}
return {
restrict: 'A',
scope: {
clear: '&'
},
link: function (scope, element) {
element.keyup(function (event) {
if (isEscapeKey(event.keyCode)) {
scope.clear();
}
});
}
};
});
I've got the sample code here: http://jsfiddle.net/darrenthomas/cbcpq/1/
I would like to point out that I'm not an experienced JavaScript programmer and that I'm new to AngularJS. I've also looked at How do I pass multiple attributes into an Angular.js attribute directive? but am unable to get working solution.