Disable AngularJS from automatically trimming inputs

You will almost always want Angular to automatically trim your inputs. I can think of few situations where trailing whitespace is useful for data.

There may come a time however that you will need that trailing whitespace. As an example, I needed it when I was watching the change of an input and I was listening for the last character to be a space. Since Angular trimmed that whitespace I couldn’t see the trailing space and couldn’t execute my code.

Now angular supports an optional directive for turning off the trim on a given input. All you need to do is this:

<input type="text" ng-model="trimDemo" ng-trim="false">

Now if the user types “Trailing whitespace ” in the input you will get that exact string in the model.

Share Button

Tagged under: ,

Back to top