Skip to content

Instantly share code, notes, and snippets.

@leongaban
Last active November 1, 2015 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leongaban/9512645ab01e74bb0663 to your computer and use it in GitHub Desktop.
Save leongaban/9512645ab01e74bb0663 to your computer and use it in GitHub Desktop.
focusMe custom attribute Directive
/*global angular*/
////////////////////////////////////////////////////////////////////////////////
/**
* @name focusMe
* @namespace Directives
* @desc Select search input after clicking on search icon
*/
(function() {
angular.module('focusMeDirectives', [])
// Main search focus:
.directive('focusSearch', ['$timeout', '$parse', function($timeout, $parse) {
return {
scope: { trigger: '=focusSearch' },
link: function(scope, element, attrs) {
var model = $parse(attrs.focusSearch);
// scope.$watch(model, function(value) {
scope.$watch('trigger', function(value) {
// console.log('value=',value);
// console.log('element[0] = ',element[0]);
if (value === true) {
$timeout(function() {
element[0].focus();
});
}
});
}
}
}])
// Tag search focus:
.directive('focusTags', ['$timeout', '$parse', function($timeout, $parse) {
return {
scope: { trigger: '=focusTags' },
link: function(scope, element, attrs) {
var model = $parse(attrs.focusTags);
scope.$watch('trigger', function(value) {
if (value === true) {
$timeout(function() {
element[0].focus();
});
}
});
}
}
}])
})();
/*
MARKUP
<div class="search-input" ng-show="ph.searchOpened">
<input type="text"
placeholder="Search"
ng-model="ph.searchInput"
focus-search="ph.searchOpened"
typeahead-wait-ms="500"
typeahead-min-length="2"
typeahead-wait-ms="500"
typeahead="search as search.value for search in ph.typingSearch($viewValue)">
</div>
DIRECTIVE CONTROLLER:
function toggleSearch() {
vm.searchOpened = !vm.searchOpened;
if (vm.searchOpened === false) {
vm.searchInput = '';
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment