Re: [angular.js] Angularjs will trim the content of a model, how to disable it?

5,935 views
Skip to first unread message

Pawel Kozlowski

unread,
Jun 8, 2012, 2:01:03 PM6/8/12
to ang...@googlegroups.com
Hi!

On Fri, Jun 8, 2012 at 4:09 PM, Freewind <nowi...@gmail.com> wrote:
> See the demo: http://jsfiddle.net/Freewind/A8VzX/
>
> If I input something which has leading or ending spaces, it will be trimmed
> when displaying it.

Bumped into a very similar issue yesterday...
Had a look at the angular sources and a listener in the textInputType
effectivelly trims input's value:

https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L373

> How to let angularjs not do this?

The only way I can see is by writing a custom directive :-( But I
would be really curious to know why this trim call is in place.... For
me it is counter-intuitive but can see that was a consious decision
from the angular team as there is a test for this:
https://github.com/angular/angular.js/blob/master/test/ng/directive/inputSpec.js#L368

Would really love to know why there is trimming by default...

Cheers,
Pawel

Vojta Jína

unread,
Jun 12, 2012, 3:51:46 AM6/12/12
to ang...@googlegroups.com
Does anyone have any issue with removing the trimming ?
I think we might remove it, I only remember people complaining about it :-D

V.

On Sun, Jun 10, 2012 at 8:33 AM, Freewind <nowi...@gmail.com> wrote:
I still want to know if there is any easy way to let angular not trim the value, please help~


On Friday, June 8, 2012 10:09:40 PM UTC+8, Freewind wrote:
See the demo: http://jsfiddle.net/Freewind/A8VzX/

If I input something which has leading or ending spaces, it will be trimmed when displaying it. 

How to let angularjs not do this?
On Friday, June 8, 2012 10:09:40 PM UTC+8, Freewind wrote:
See the demo: http://jsfiddle.net/Freewind/A8VzX/

If I input something which has leading or ending spaces, it will be trimmed when displaying it. 

How to let angularjs not do this?
On Friday, June 8, 2012 10:09:40 PM UTC+8, Freewind wrote:
See the demo: http://jsfiddle.net/Freewind/A8VzX/

If I input something which has leading or ending spaces, it will be trimmed when displaying it. 

How to let angularjs not do this?
On Friday, June 8, 2012 10:09:40 PM UTC+8, Freewind wrote:
See the demo: http://jsfiddle.net/Freewind/A8VzX/

If I input something which has leading or ending spaces, it will be trimmed when displaying it. 

How to let angularjs not do this?
On Friday, June 8, 2012 10:09:40 PM UTC+8, Freewind wrote:
See the demo: http://jsfiddle.net/Freewind/A8VzX/

If I input something which has leading or ending spaces, it will be trimmed when displaying it. 

How to let angularjs not do this?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/mg8OKPlS8OkJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

j...@linnebank.nl

unread,
Jul 23, 2012, 1:05:20 PM7/23/12
to ang...@googlegroups.com
Well, better than how it works now.
A trim attribute could help out as well as Freewind suggested.
But I would default it to false (so the confusion disappears and it's more inline with HTML/DOM).
And if the attribute is value-less, it could perhaps mean the same as: trim="true".

So <input name="test" ng-trim> would enable trimming.

Thanks for the great lib.

Jan
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

wahyu...@gmail.com

unread,
Jul 23, 2012, 2:14:39 PM7/23/12
to ang...@googlegroups.com, j...@linnebank.nl

It's not related to your problem but seeing that you are trying to create a markdown editor, maybe this can help you: http://www.youtube.com/watch?v=A6wq16Ow5Ec

gregp...@gmail.com

unread,
Aug 27, 2012, 5:49:53 PM8/27/12
to ang...@googlegroups.com
That's what I want. Just the option to remove it.

On Tuesday, June 12, 2012 3:55:42 AM UTC-4, Freewind wrote:
I think we can add an option to control it, e.g.

    <input ng-model="name" trim="false" />

Then the value won't be trimmed. If attribute "trim" is not provided, it will trim.

PS: we can provide more options to the "trim", e.g. trim="start/end/true/false"

Dave Stewart

unread,
Sep 19, 2012, 2:39:27 PM9/19/12
to ang...@googlegroups.com
This is a real pain, right on my first app!

I see this pull request, but it's starting to get a bit unwieldy with "ng-no-trim"


I can't see it in the 1.1 release either:


Any ideas anyone?

Dave Stewart

unread,
Sep 19, 2012, 2:51:17 PM9/19/12
to ang...@googlegroups.com
Screw it. Hack time:

If you remove the trim() in textInputType() on line 11158 of http://code.angularjs.org/1.1.0/angular.js, you're back in the game :)

Dave Stewart

unread,
Sep 19, 2012, 2:59:58 PM9/19/12
to ang...@googlegroups.com
Actually, more useful is to change line 11161...

var value = trim(element.val());

...to...

var value = attr.trim == 'false' ? element.val() : trim(element.val());

Then you can set an optional "trim" attribute on the input like so:

<input trim="false" value="" />

This should get you through until the updates make their way into the release.

gdan...@gmail.com

unread,
Dec 9, 2012, 6:26:52 AM12/9/12
to ang...@googlegroups.com
It seems that your internal Angularjs apps use some kind of single sign on. With unmodified Angluarjs it is impossible to create a correct login form. How one should avoid trimming passwords?



On Tuesday, June 12, 2012 9:51:46 AM UTC+2, Vojta Jína wrote:

Pawel Kozlowski

unread,
Dec 9, 2012, 7:20:27 AM12/9/12
to ang...@googlegroups.com
Hi!

On Sun, Dec 9, 2012 at 12:26 PM, <gdan...@gmail.com> wrote:
> It seems that your internal Angularjs apps use some kind of single sign on.
> With unmodified Angluarjs it is impossible to create a correct login form.
> How one should avoid trimming passwords?

Version 1.1.1 of AngularJS has a built-in support for the ngTrim directive:
https://github.com/angular/angular.js/blob/master/CHANGELOG.md
https://github.com/angular/angular.js/commit/d519953a4b219035587e3fcb2e9cc52e02b408ca
http://code.angularjs.org/1.1.1/docs/api/ng.directive:input.text

Cheers,
Pawel

--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Witold Szczerba

unread,
Dec 9, 2012, 7:44:13 AM12/9/12
to ang...@googlegroups.com

Hi,
ngTrim is just an attribute, not a directive. Input directive check it to decide if the value should be trimmed or not.

Regards,
Witold Szczerba
---
Sent from my mobile phone.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.

Pawel Kozlowski

unread,
Dec 9, 2012, 7:50:11 AM12/9/12
to ang...@googlegroups.com
Hi!

On Sun, Dec 9, 2012 at 1:44 PM, Witold Szczerba <pljos...@gmail.com> wrote:

> ngTrim is just an attribute, not a directive. Input directive check it to
> decide if the value should be trimmed or not.

Ah, yes, sorry. Thnx for being vigilant :-)
Anyway, the usage is still the same: <input ng-model="myModel" ng-trim="false">
Reply all
Reply to author
Forward
0 new messages