angular input formatter/parser directive and interpolated attributes?

246 views
Skip to first unread message

Jeremy Bell

unread,
Feb 15, 2013, 11:37:21 AM2/15/13
to ang...@googlegroups.com

I am attempting to write a configurable directive that is applied to an input element, which requires ngModel, and which adds a parser and a formatter function for ngModel.

The problem I'm having is that I can't seem to pass interpolated values into the directive while simultaneously supporting ngModel binding. For instance, I want to be able to use my directive in one of two ways: by passing literal arguments (e.g. my-directive="120"), or interpolated arguments (e.g. my-directive="{{foo}}"). 

If I read the attributes argument of the link function in my directive definition, I can get the value of attributes.myDirective when I pass a literal, but when I attempt to use an interpolated value, myDirective is undefined. Now, if I add an isolated scope to the directive definition with myDirective: '@', then scope.myDirective is defined and interpolated in the scenarios above, but now ngModel is broken. My parser/formatter functions are passed undefined for their input arguments. Any ideas how I can fix this and support literal arguments and interpolated arguments and ngModel binding in the same directive?

Pawel Kozlowski

unread,
Feb 15, 2013, 1:21:53 PM2/15/13
to ang...@googlegroups.com
Jeremy,

It is not 100% clear from your description what the new directive
should be doing and what are the exact problems you are facing.
Sending a plunker with the life code would increase your chances of
getting a helpful answer.

The closest thing that resembles a directive from your description is
ui-validate from angular-ui, but then again, might be wrong here.

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Looking for bootstrap-based widget library for AngularJS?
http://angular-ui.github.com/bootstrap/

Jeremy Bell

unread,
Feb 19, 2013, 1:02:58 PM2/19/13
to ang...@googlegroups.com
Thanks Pawel. I think I was able to work around this problem by using the $parse service and using it to get/set values on the scope, passing the expression in as an attribute of the directive in string form. The only difference is that, for hard-coded values, I have to wrap the value in single quotes:
<input my-directive="'foo'" /> which actually makes sense.

Thanks!
Jeremy Bell
Sleepy Daddy Software™
---------------------------------------------------------------------------------------------------------
Have a little one? Try Giggle Pad© for Windows Phone 7, a fun and educational game for children 9 months and older:
 
Does your brand new Windows Phone 7 have dead pixels or screen discoloration? Find out with Pixel Checkup© for Windows Phone 7:
 
Giggle Pad and Pixel Checkup are copyright © 2010 Jeremy Bell and Sleepy Daddy Software™
Reply all
Reply to author
Forward
0 new messages