Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use directives that use ngModel ? #50

Closed
pierre-michalet opened this issue Sep 22, 2014 · 6 comments
Closed

How to use directives that use ngModel ? #50

pierre-michalet opened this issue Sep 22, 2014 · 6 comments

Comments

@pierre-michalet
Copy link

Hi Brian,

I previously had the directive below that converts US numbers into French one

.directive('smartFloat', function($filter) {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {t
            ctrl.$parsers.unshift(function(viewValue) {
                return viewValue.replace(',', '.');
            });

            ctrl.$formatters.unshift(function(modelValue) {
                return $filter('number')(modelValue);
            });
        }
    };
})

The problem is that I can't use this directive with my new text-field anymore, because ngModel is required

The only solution I came up with is to create a new directive based on textField (actually it's a copy/paste ... apparently it is not possible to inherit directives ) with a new template (also copy/pasted from the text-field template ...) where I can put my directive on the input

It is not very elegant nor maintainable, do you have another way to do that ?

Thanks !

@bvaughn
Copy link
Owner

bvaughn commented Sep 22, 2014

Hi @pierre-michalet.

I can't think of a graceful way to do this with formFor at the moment.

I believe you're essentially requesting parser/formatter support for the textField directive?

Let me think about it a bit.

@pierre-michalet
Copy link
Author

Yes, for that special directive.

But I use https://github.com/angular-ui/ui-select2 and the directive select2 has to be put on the input so it is not limited to parser/formatter

Maybe we can have a template attribute that we can use to specify another template than the default one ? We would still need to copy/paste the template but not the directive
Something like

<text-field template="smart-float-field.html"></text-field>

Or, better, we can copy some attributes from the text-field to the input

<text-field input-directives="{'smartFloat': {}, 'anotherDirective': {option: 1}}"></text-field>

@bvaughn
Copy link
Owner

bvaughn commented Sep 23, 2014

Oh I see. Interesting suggestion! So rather than having to override the whole field template, you could just override the <input> portion (kind of how the label and error message are individually overridable). I like it!

That feature will go out in my next release. :)

@pierre-michalet
Copy link
Author

Thanks a lot !

bvaughn pushed a commit that referenced this issue Sep 25, 2014
…late into overridable partials to allow for easier addition of custom directives requiring access to ngModel
@bvaughn
Copy link
Owner

bvaughn commented Sep 25, 2014

To override either the single-line <input> template or the multiline <textarea> template you can just do the following:

$templateCache.put('form-for/templates/text-field/_input.html', '...');
$templateCache.put('form-for/templates/text-field/_textarea.html', '...');

I recommend forking the existing templates as a starting point though, to make sure the necessary bindings are all in place.

This will go out in the next release (hopefully within a half hour or so). Thanks for your patience.

@bvaughn bvaughn closed this as completed Sep 25, 2014
@bvaughn
Copy link
Owner

bvaughn commented Sep 25, 2014

Check out release 1.4.3

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants