Instead of doing this on the controller, you should use a directive like this:
app.directive('restrict', function($parse) { return { restrict: 'A', require: 'ngModel', link: function(scope, iElement, iAttrs, controller) { scope.$watch(iAttrs.ngModel, function(value) { if (!value) { return; } $parse(iAttrs.ngModel).assign(scope, value.toLowerCase().replace(new RegExp(iAttrs.restrict, 'g'), '').replace(/\s+/g, '-')); }); } } });
And then use it on input as follows:
<input restrict="[^a-z0-9\-\s]" data-ng-model="slug" ...>
jsFiddle: http://jsfiddle.net/9qxFK/5/
bmleite
source share