Skip to content

Instantly share code, notes, and snippets.

@robertu7
Created March 12, 2015 07:15
Show Gist options
  • Save robertu7/605612cce5b98c3c96bf to your computer and use it in GitHub Desktop.
Save robertu7/605612cce5b98c3c96bf to your computer and use it in GitHub Desktop.
AngularJS Directive:在 Mobile Browser 提交表單後自動隱藏 Keyboard
/**
* Usage:
* <form hide-keyboard></form>
*/
angular.module('hideKeyboardDirective', [])
.directive('hideKeyboard', ['$rootScope', function ($rootScope){
return {
restrict: 'A',
controller: function (){
// 檢測是否為手機瀏覽器
// https://stackoverflow.com/questions/11381673/detecting-a-mobile-browser
function detectmob() {
var ua = $window.navigator.userAgent;
if(
ua.match(/Android/i)
|| ua.match(/webOS/i)
|| ua.match(/iPhone/i)
|| ua.match(/iPad/i)
|| ua.match(/iPod/i)
|| ua.match(/BlackBerry/i)
|| ua.match(/Windows Phone/i)
){
return true;
}
else {
return false;
}
}
$rootScope.isMobile = detectmob()
},
link: function (scope, elem, attr){
var inputElem = elem.find('input');
if ($rootScope.isMobile){
elem.bind('submit', function (){
inputElem[0].blur()
})
}
}
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment