Created
April 28, 2015 17:06
-
-
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.
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
(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