Last active
November 1, 2015 21:13
-
-
Save leongaban/9512645ab01e74bb0663 to your computer and use it in GitHub Desktop.
focusMe custom attribute Directive
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
/*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