Skip to content

Instantly share code, notes, and snippets.

@heavysixer
Created April 28, 2015 17:06
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 heavysixer/fdfcda1a2ff15a82861d to your computer and use it in GitHub Desktop.
Save heavysixer/fdfcda1a2ff15a82861d to your computer and use it in GitHub Desktop.
Two-way directive for stripping HTML from a input field. It will filter HTML as the user types it or as the variable linked to ngModel changes.
(function() {
'use strict';
/**
* @ngdoc directive
* @name stripHtml
* @module humansized.directives.stripHtml
* @restrict A
*
* @description
*
* A directive that strips the HTML as part of the text element validation pipeline
*
* @usage
*
* %textarea(ng-model="org.description" columns="1" md-maxlength="250" strip-html)
*/
var stripHtml = function() {
return {
require: 'ngModel',
restrict: 'A',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
var transformedInput = String(inputValue).replace(/<[^>]+>/gm, '');
if (transformedInput !== inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
modelCtrl.$formatters.push(function(data) {
if(angular.isDefined(data)){
return String(data).replace(/<[^>]+>/gm, '');
} else {
return data;
}
});
}
};
};
angular.module('humansized.directives')
.directive('stripHtml', stripHtml);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment