Is there an angular way to apply a filter an <input type="text"> to an incoming ngModel value?

22,286 views
Skip to first unread message

Dan Rybij

unread,
Nov 7, 2012, 3:20:33 PM11/7/12
to ang...@googlegroups.com
It's very handy to be able to make filters to format expressions:

<div>{{my_datetime | datetime : 'MM/dd/yyyy'}}</div>

But I can't seem to find an elegant angular way to format an incoming value (via ngModel) in a form widget:

<input type="text" ng-model="my_datetime" format-datetime="MM/dd/yyyy"/>

I tried to make a directive which will work with <input> as well as <textarea> and can't seem to make the $watch work.

I feel like I'm missing something obvious. Please help!

Peter Bacon Darwin

unread,
Nov 7, 2012, 3:39:10 PM11/7/12
to ang...@googlegroups.com
Yes!  You can hook into the $formatters and $parsers on ngModelController.  Take a look at the section called "Implementing custom form control (using ngModel)" in the docs at http://docs.angularjs.org/guide/forms
Pete


--
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.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Dan Rybij

unread,
Nov 7, 2012, 5:59:35 PM11/7/12
to ang...@googlegroups.com
Excellent! Thank you!

Dan Rybij

unread,
Nov 8, 2012, 1:29:45 PM11/8/12
to ang...@googlegroups.com
Looking at formatters now... missed that part of your post. Thanks


On Wednesday, November 7, 2012 3:39:34 PM UTC-5, Peter Bacon Darwin wrote:
Reply all
Reply to author
Forward
0 new messages