sonicwong / ionic-input-clearable Goto Github PK
View Code? Open in Web Editor NEWAn Ionic AngularJS directive for adding a clear button to an input element.
An Ionic AngularJS directive for adding a clear button to an input element.
Feel free to add this one to your directive in order to prevent the input to blur when the user click on the clear button :
$scope.clearInputField = function ( event ) {
$scope.ngModel = '';
function preventBluring() {
element.focus();
$element.unbind( 'blur', preventBluring );
}
//keep focus on the field when click on the clear button
$element.bind( 'blur' , preventBluring );
};
This is the patch in order to prevent the error throw when $digest is called whereas it's already in progress :
function refresh() {
$timeout( function () {
$scope.$digest();
});}
$element.bind( 'focus', refresh );
$element.bind( 'blur', refresh );
Sorry about that, it was from my code :p
Does the clearable "x" appear if the value of the input is changed dynamically (programmatically) from empty to some other value?
Looks very nice. For better usage in bower and stuff, it would be nice, if you could provide tags as releases and maybe additonally provide releases in bower library.
When i use visual studio 2013 for Web Essentials,bundle "sw2.ionic.input-clearable.js" get an error,
so i change line 7 and 13 to fix it.Dose anybody occurred same problem?
And what about a default config ? :)
In my app, I prefer that the button appears only when the field is focused, but, in some field, I want to see the clear button even if the text field is not focused.
So I suggest something like that :
angular.module('sw2.ionic.input-clearable', [])
.provider('ionicInputClearableConfig', function() {
this.defaultDisplayType = "filled";
this.$get = function () {
return this;
};
})
/**
* ionicInputClearable might have 3 values :
- always (the button is always visible even if it's empty)
- focused (the button appear when the field is focused and filled)
- [default] filled (the button appear when the field is filled)
*/
.directive('ionicInputClearable', function ($compile) {
return {
restrict: 'A',
scope: {
ngModel: '='
},
controller: function ($scope, $element, $attrs, ionicInputClearableConfig) {
var displayType = ($attrs.ionicInputClearable.length > 0 ? $attrs.ionicInputClearable : ionicInputClearableConfig.defaultDisplayType);
var element = $element[0];
//Parent element should be position:relative to make clear button in correct positon
$element.parent().css( "position", "relative" );
//ng-hide class used to fix init flash problem
$scope.clearBtn = angular.element('<a tabindex="-1" ng-cloak class="ng-hide input-clear-btn button button-icon icon ion-close" ng-click="clearInputField()" ng-hide="isInputFieldEmpty()"></a>');
$compile($scope.clearBtn)($scope);
$element.after($scope.clearBtn);
$scope.clearInputField = function () {
$scope.ngModel = '';
}
$element.bind( 'focus', function() {
$scope.$digest();
});
$element.bind( 'blur', function() {
$scope.$digest();
});
$scope.isInputFieldEmpty = function () {
switch ( displayType ) {
case "always" :
return false;
case "focused" :
return (document.activeElement !== element || element.value == '' || element.value == null );
default :
return (element.value == '' || element.value == null);
}
}
}
}
})
Usage :
//Give the ability to choose the default strategy to display the button
var app = angular.module( 'myApp' , ['ionic', 'sw2.ionic.input-clearable'] );
app.config(function(ionicInputClearableConfigProvider) {
ionicInputClearableConfigProvider.defaultDisplayType = "focused";
});will display only when focused (default configured behavior)
<input ionic-input-clearable ng-model="form.searchText" type="search">
button is always displayed
<input ionic-input-clearable="always" ng-model="form.searchText" type="search">
button is display when the textfield is filled
<input ionic-input-clearable="filled" ng-model="form.searchText" type="search">
button is display when the textfield is focused
<input ionic-input-clearable="focused" ng-model="form.searchText" type="search">
The button is not showing until the email is correct :
<input ionic-input-clearable name="email" ng-model="form.email" type="email" placeholder="email" required>
fix :
$scope.isInputFieldEmpty = function () { return ($element[0].value == '' || $element[0].value == null ); //return ($scope.ngModel == '' || $scope.ngModel == null ); }
The cross will be available only when the input has the focus (like in ios) :
var element = $element[0];
$element.bind( 'focus', function() {
$scope.$digest();
});
$element.bind( 'blur', function() {
$scope.$digest();
});
$scope.isInputFieldEmpty = function () {
return (document.activeElement !== element || element.value == '' || element.value == null );
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.