angular / material

Material design for AngularJS
https://material.angularjs.org/
MIT License
16.54k stars 3.39k forks source link

md-maxlength character count doesn't update when changed in controller #1983

Closed johncvieira closed 8 years ago

johncvieira commented 9 years ago

If an input field has a md-maxlength showing the character count of the field, (ex. 34/40). And the form is submitted, and I clear the field in the controller (vm.task.field = ''). The counter will remain at 34/40 with no text in the field.

Related to #1870

gkalpak commented 9 years ago

This is an actual issue. Has been also mentioned in #1903.

breeze4 commented 9 years ago

The key line of code seems to be right here:

https://github.com/angular/material/blob/master/src/components/input/input.js#L323

Instead of using element.val() to get the latest value of the input field, why not use ngModelCtrl? Current: charCountEl.text( ( element.val() || value || '' ).length + '/' + maxlength ); Proposed: charCountEl.text( ( ngModelCtrl.$modelValue || '' ).length + '/' + maxlength );

Is it okay to reference ngModelCtrl.$modelValue in this context?

If that seems like a valid change, I'd like to try my hand at a PR fixing it with a corresponding test.

batressc commented 9 years ago

I created this example in codepen.io (http://tinyurl.com/ngfhxcu) where I "override" the md-maxlength directive and have added the proposed change by @breeze4 for better reference.

Please consider include this change and make the respective test framework

zargold commented 9 years ago

@batressc nice codepen but the problem with this is once you go over the md-maxlength (when I just tried doing that it says you are persistently at 0/150 or whatever rather than 159/150) so it gives incorrect information the length measurement became off.

gkalpak commented 9 years ago

Indeed, using the $modelValue won't show correct info when the input value is invalid, since (by default) in that case the $modelValue is set to undefined.

julienpa commented 8 years ago

Since it has been moved to the backlog, does it mean it won't be fixed anytime soon?

smurugavel commented 8 years ago

+1. I see another workaround suggested in here but I haven't tried it 1870

corentin-gautier commented 8 years ago

+1

nestabur commented 8 years ago

+1

pablozandona commented 8 years ago

+1

ogaihtorecic commented 8 years ago

In your clearForm function:

$timeout(function() { model.field = ""; }, 30);

Worked for me.

MadJlzz commented 8 years ago

+1

DevinWatson commented 8 years ago

+1

smurugavel commented 8 years ago

Hello @ThomasBurleson. A resolution on closing this item will be much appreciated. This and most other items that were closed are missing resolution.

ThomasBurleson commented 8 years ago

This issue is closed as part of our deprecation effort. For details, see our post Spring Cleaning 2016.

Which other closed, deprecated issues are missing the ^^ comment ?

devversion commented 8 years ago

Referencing #8351

TiboQc commented 8 years ago

I managed to find a workaround based on what @ogaihtorecic said (which didn't work for me), using Angular 1.3.20 and Angular-Material 1.0.5:

$scope.model.field = '';
$timeout(function() {
    $scope.form.fieldName.$setViewValue(''); // resets the <input> value which updates the counter
    $scope.form.fieldName.$setUntouched(); // resets validation on the field
},0);
ghost commented 8 years ago

I wrote a simple code that could solve this problem

in your view: md-maxlength="vmCtrl.getMaxLen(30)"

in your controller:

var fakeMaxLen; vmCtrl.getMaxLen = function (value) { if (fakeMaxLen) return fakeMaxLen; return value; };

// call this function after your data model was changed and you need to update char counter function callMeWhenYouNeedToResetCharCounter(){ fakeMaxLen = 100000; $timeout(function(){ fakeMaxLen =undefined; vmCtrl.form.$setPristine(); vmCtrl.form.$setUntouched(); }); }

// you can disable animation of .md-errors-spacer class and set visibility to hidden, when process started // and return back to original state after the process finalized (in timeout function) // it's just for smoother working and prevent char counter flickering

himvins commented 7 years ago

codepen - (http://tinyurl.com/ngfhxcu) given by @batressc is working fine for the issue but it has one issue mentioned by @zargold . By just modifying the codepen with one condition It is working fine for me. Try this codepen with below renderCharCount function.

function renderCharCount(value) {
                            if(ngModelCtrl.$modelValue !== "")
                                    charCountEl.text( ( element.val() || value || '' ).length + '/' + maxlength );        
                            else
                                    charCountEl.text((ngModelCtrl.$modelValue || '').length + '/' + maxlength);

                            return value;
                        }
tgelu commented 7 years ago

+1