Help with directive using ng-model

309 views
Skip to first unread message

mooyah

unread,
Jul 2, 2013, 5:41:25 PM7/2/13
to ang...@googlegroups.com
Hello,

I have a plunker that shows the problem I'm having: http://plnkr.co/edit/AhZxBJOjWbtGRQIJfyjP?p=preview

I'm trying to get the directive to format the input nicely, but also persist to the ng-model="imanumber".

TYIA,
Joel

mooyah

unread,
Jul 2, 2013, 5:45:30 PM7/2/13
to ang...@googlegroups.com
Also, how do I put a suffix="%" into the attributes for this directive? Angular thinks that it is a operator, so I'm guessing I need to escape it somehow?

Leonardo Campos

unread,
Jul 2, 2013, 6:33:50 PM7/2/13
to ang...@googlegroups.com
First of all, you must use a camelCase directive name and use it as snake-case.
I don't like to work with isolated scope as you did (scope: { })

I did some modifications and you can see the example at this url:



Anyone know how to run the line below in a best way (after link or after page load event without using jQuery.ready)?

    jQuery(input).autoNumeric('init', options)

If I remove "scope.$watch" nothing happens until the field value is edited or a blur event is dispatched.

Thanks
-- 
Leonardo da Silva Campos
"Keep distance, coder coding!"


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Joel DuBien

unread,
Jul 2, 2013, 6:41:56 PM7/2/13
to ang...@googlegroups.com
Thanks for the reply. I was using isolate scope to pass the options through, but I see using the attrs reference works as well.

However, the data binding on the model is still not working. If I update the input, the text in the span doesn't update. Do you know how to fix that?


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/jG1YteHU6-4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

Leonardo Campos

unread,
Jul 3, 2013, 7:36:15 AM7/3/13
to ang...@googlegroups.com
In this case your directive must have 'ngModel' as required and the 'Controller' parameter must be added in your link function (as 4th parameter), but I think this plugin will not work with angular.js.

Take a look at "Angular UI Mask" to use it as reference.


You can find Angular directives for currency format 'ready-to-use' on Google.

Att.

mooyah

unread,
Jul 3, 2013, 9:48:58 AM7/3/13
to ang...@googlegroups.com
I managed to make it work. Thanks for the help with the requiring of ngModel.

For anyone else having troubles with this, check it out:

Leonardo Campos

unread,
Jul 3, 2013, 9:56:56 AM7/3/13
to ang...@googlegroups.com
Pretty cool, mooyah!

I have never worked with ngModel, but I read on some sites about it.
I'll save your solution to follow it when I need to create this kind of directive in a near future.

Thanks by your reply!

Reply all
Reply to author
Forward
0 new messages