Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

md-maxlength doesn't update immediately if model content is updated in javascript #1870

Closed
Sam-Davis opened this issue Mar 11, 2015 · 12 comments
Assignees
Labels
has: Pull Request A PR has been created to address this issue

Comments

@Sam-Davis
Copy link

Steps to reproduce:

  1. create a textarea with md-maxlength="500" ng-model="modelitem.description"
  2. create a button with a function that updates modelitem.description to be null

issue - the couter of characters used next to the text area will not update, it will stay as 32/500 for example. It will only update once the user starts typing again.

This is in version 0.7.1

@bradmartin
Copy link

+1 still present in current master.

$setPristine(); and $setUntouched() have no effect.

@gauravbhavsar
Copy link

+1 (any work-around for now ?)

@gauravbhavsar
Copy link

here is the plunker for work around.

@ThomasBurleson ThomasBurleson modified the milestones: post-1.0 , Backlog Sep 4, 2015
@scspijker
Copy link

👍 Still not working. If like me, you have a clearForm function, you might not want the watch from gauravbhavsar's example. Simply make your md-maxlength get the length from your controller:

<input type="text" ng-model="ctrl.description" md-maxlength="ctrl.maxLengthDescription">

And then in your clearForm() method, reset the maxLength (like the watch does) (example is ES6):

_clearForm(form) {
  this.description = "";

  // Known issue, md-maxlength does not update on model change
  // https://github.com/angular/material/issues/1870
  this.maxLengthDescription = 500;
  this.timeout(() => {
    this.maxLengthDescription = 30;
  });

  form.$setPristine();
  form.$setUntouched();
}

@andrew-cunliffe
Copy link

👍

An easy work around for this is to also include the ng-maxlength attribute like so...

    <md-input-container>
        <label>Description</label>
        <textarea name="description"
                  required="required"
                  md-maxlength="500"
                  ng-maxlength="500"
                  ng-model="ctrl.summaryDescription"></textarea>

        <div ng-messages="form.description.$error">
            <div ng-message="required">Description is required</div>
            <div ng-message-exp="['md-maxlength', 'maxlength']">Description is too long</div>
        </div>
    </md-input-container>

... this appears to 'work' better however highlights another issue that I have raised here #5853

@ThomasBurleson ThomasBurleson modified the milestones: post-1.0 , Backlog Jan 5, 2016
@claire921
Copy link

+1

@sunil-ideas2it
Copy link

Anyone got this thing fixed. I am facing the same issue

@ThomasBurleson ThomasBurleson modified the milestones: Backlog, Deprecated Apr 20, 2016
@scspijker
Copy link

@ThomasBurleston this is fixed in a new release?

@Steve-Mc
Copy link

Steve-Mc commented May 5, 2016

Still happening for me.

@devversion devversion reopened this May 5, 2016
@devversion
Copy link
Member

@steve-mcqs This issue was closed by our Spring Cleaning.

But as I'm able to reproduce that bug, I'll reopen that issue.

@devversion devversion self-assigned this May 5, 2016
@devversion devversion modified the milestones: - Backlog, Deprecated May 5, 2016
devversion added a commit to devversion/material that referenced this issue May 5, 2016
* Currently the md-maxlength char counter was not updated properly and listening on the different events is unnecessary. ngModelController will already update the model, when the `input` event gets called.
  Additonally we can cover all updates in the $validator itself, because it will always update, when changes get applied to the model.

Fixes angular#1870.
@devversion devversion added the has: Pull Request A PR has been created to address this issue label May 5, 2016
@ThomasBurleson ThomasBurleson modified the milestones: - Backlog, Deprecated May 26, 2016
@devversion devversion modified the milestones: - Backlog, Deprecated May 27, 2016
devversion added a commit to devversion/material that referenced this issue Jul 14, 2016
* Currently the md-maxlength char counter was not updated properly and listening on the different events is unnecessary. ngModelController will already update the model, when the `input` event gets called.
  Additonally we can cover all updates in the $validator itself, because it will always update, when changes get applied to the model.

Fixes angular#1870.
devversion added a commit to devversion/material that referenced this issue Jul 14, 2016
* Currently the md-maxlength char counter was not updated properly and listening on the different events is unnecessary. ngModelController will already update the model, when the `input` event gets called.
  Additonally we can cover all updates in the $validator itself, because it will always update, when changes get applied to the model.

Fixes angular#1870.
@pawelbujna
Copy link

pawelbujna commented Aug 30, 2016

Ok, I don't know if it helps You, but for me fix like this works:

angular.element('#html-element-' + name).triggerHandler('input');

This is a part of directive so thats the reason i use name variable. You just have to trigger(Handler) input event.

@ghost
Copy link

ghost commented Sep 16, 2016

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

kara pushed a commit that referenced this issue Oct 14, 2016
* Currently the md-maxlength char counter was not updated properly and listening on the different events is unnecessary. ngModelController will already update the model, when the `input` event gets called.
  Additonally we can cover all updates in the $validator itself, because it will always update, when changes get applied to the model.

Fixes #1870.
@Splaktar Splaktar removed this from the - Backlog milestone Feb 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has: Pull Request A PR has been created to address this issue
Projects
None yet
Development

No branches or pull requests