Skip to content

Instantly share code, notes, and snippets.

@harishkotra
Created March 19, 2017 04:39
Show Gist options
  • Save harishkotra/f4213a21e7da1e3cb1e08148ff15438c to your computer and use it in GitHub Desktop.
Save harishkotra/f4213a21e7da1e3cb1e08148ff15438c to your computer and use it in GitHub Desktop.
Ionic 1: Directive to add search bar in the Nav/Action bar of the app
.directive('searchBar', [function () {
return {
scope: {
ngModel: '='
},
require: ['^ionNavBar', '?ngModel'],
restrict: 'E',
replace: true,
template: '<ion-nav-buttons side="right">'+
'<div class="searchBar item-input-inset">'+
'<div class="searchTxt" ng-show="ngModel.show">'+
'<div class="bgdiv"></div>'+
'<div class="bgtxt">'+
'<input type="text" placeholder="Search..." ng-model="data.searchterm" ng-enter="search()">'+
'</div>'+
'</div>'+
'<i class="icon placeholder-icon" ng-click="ngModel.txt=\'\';ngModel.show=!ngModel.show"></i>'+
'</div>'+
'</ion-nav-buttons>',
compile: function (element, attrs) {
var icon=attrs.icon
|| (ionic.Platform.isAndroid() && 'ion-android-search')
|| (ionic.Platform.isIOS() && 'ion-ios7-search')
|| 'ion-search';
angular.element(element[0].querySelector('.icon')).addClass(icon);
return function($scope, $element, $attrs, ctrls) {
var navBarCtrl = ctrls[0];
$scope.navElement = $attrs.side === 'right' ? navBarCtrl.rightButtonsElement : navBarCtrl.leftButtonsElement;
};
},
controller: ['$scope','$ionicNavBarDelegate', function($scope,$ionicNavBarDelegate){
var title, definedClass;
$scope.$watch('ngModel.show', function(showing, oldVal, scope) {
if(showing!==oldVal) {
if(showing) {
if(!definedClass) {
var numicons=$scope.navElement.children().length;
angular.element($scope.navElement[0].querySelector('.searchBar')).addClass('numicons'+numicons);
}
title = $ionicNavBarDelegate.getTitle();
$ionicNavBarDelegate.setTitle('');
} else {
$ionicNavBarDelegate.setTitle(title);
}
} else if (!title) {
title = $ionicNavBarDelegate.getTitle();
}
});
}]
};
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment