Created
February 3, 2017 20:58
-
-
Save m0sk1t/c831736b0b48e05497c41108e211bb98 to your computer and use it in GitHub Desktop.
MyAutocompleteDirective
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div ng-app="ExampleAutocomplete"> | |
<div ng-controller="ExampleCtrl"> | |
<my-autocomplete on-find="find(substr)" users="users"> | |
</my-autocomplete> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('ExampleAutocomplete', []) | |
.factory('FakeAjax', function(){ | |
var users = [ | |
{ name: 'John' }, | |
{ name: 'Joann' }, | |
{ name: 'Jorge' }, | |
{ name: 'Jack' }, | |
{ name: 'Sara' }, | |
]; | |
String.prototype.tlc = String.prototype.toLowerCase; | |
return { | |
find: function(substr) { | |
return users.filter(function(el){return el.name.tlc().indexOf(substr.tlc()) !== -1;}); | |
} | |
}; | |
}) | |
.directive('myAutocomplete', function(){ | |
return { | |
restrict: 'E', | |
scope: { | |
users: '=', | |
onFind: '&' | |
}, | |
transclude: true, | |
template: '<div ng-transclude><input type="text" ng-model="substr" ng-change="onFind({substr:substr})" /><br /><ul><li ng-repeat="u in users">{{u.name}}</li></ul></div>' | |
} | |
}) | |
.controller('ExampleCtrl', ['$scope', 'FakeAjax', function($scope, FakeAjax){ | |
$scope.users = []; | |
$scope.find = function(substr) { | |
$scope.users = FakeAjax.find(substr); | |
}; | |
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ul, li { | |
list-style-type: none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment