I have the following code which has the directive<count-characteres>
<label class="control-label"><cr/>Género <count-characters c-name="genero"></count-characters></label>
<div class="input-group">
<input type="text" name="genero" class="form-control" name="genero" equired="" ng-model="produccionGeneroSel.genero" ng-maxlength="{{validations.genero.maxlength}}">
<span class="input-group-addon"><i class="fa fa-font"></i></span>
</div>
What I am trying to do is that when I start writing to it, it input
starts a counter of remaining characters and when I exit input
it it disappears and I am not being able to call the directiva
, passing the maxlength
and it knows what input
I am writing in. I tried $watch
but I couldn't get it to work
app.directive('countCharacters', ['$injector', function ($injector) {
return{
retric:'E',
replace: true,
scope:{
cName: '=name'
},
template: function(){
return '<span>caracteres restantes</span>'; },
link: function($scope, element, $attrs){
}
};
}]);
I plan to use it for the tag name
in input
case there are several in the same form.
What you need can be done with ng-init , ng-blur and ng-focus as follows:
Tell us how it goes!
If you just want to add a counter to each input, then try using
ng-transclude
in the directive.In essence, what it does is paint the content inside the directive as if it belonged to the directive's template. So in the method
link
you do the logic to count the number of characters in the input and display it:In this example, everything inside the directive
<count-character>
will be painted inside the span<span ng-transclude></span>
, so to that we add another span that will be the character count. Then in the link function, we get the input and we add an eventkeyup
to it so that for each letter that is written, we update the counter.