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

NgModel needs a way to manually re-run the formatter chain #3407

Closed

Description

@jpsimons

See this fiddle:
http://jsfiddle.net/tPWBK/1/

Say you have a data transformation directive that requires 'ngModel' and adds to the formatter and parser chains. If the transformation is dependent on some dynamic data other than $modelValue or $viewValue, there's no way to do a watch on that data and re-run the formatter chain.

See this excerpt from https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1081

$scope.$watch(function ngModelWatch() {
  var value = ngModelGet($scope);

  // if scope model value and ngModel value are out of sync
  if (ctrl.$modelValue !== value) {

    var formatters = ctrl.$formatters,
        idx = formatters.length;

    ctrl.$modelValue = value;
    while(idx--) {
      value = formatters[idx](value);
    }

    if (ctrl.$viewValue !== value) {
      ctrl.$viewValue = value;
      ctrl.$render();
    }
  }
});

Should everything inside the (ctrl.$modelValue !== value) check be a function that's exposed publicly on NgModelController?

Activity

  1. jpsimons commented on Jul 31, 2013

    @jpsimons
    Author

    Well, I guess one could just copy that block into a directive. It's all public stuff. Would be handy to have a function to do it for you though.

  2. added this to the milestone on Jul 1, 2014
  3. added 3 commits that reference this issue on Jan 15, 2015
    48ce350
    1329cb6
    2fa87c1
  4. added a commit that references this issue on Jan 25, 2015
    6cfb2eb
  5. added 2 commits that reference this issue on Mar 4, 2015
    ae8f562
    6cc36f2
  6. bcherny commented on May 27, 2015

    @bcherny
    Contributor

    +1 would be really nice to have this. the lack of async formatters and parsers makes working with async data pretty nasty. this would help prevent some of the race conditions introduced by synchronous validation against an async data source.

    for reference, i did it very similarly to @jpsimons:

    scope.$watch('myAsyncCollection', () => {
      ngModel.$viewValue = ngModel.$formatters.reduceRight((prev, fn) => fn(prev)), ngModel.$modelValue)
      ngModel.$render()
    })
  7. s-light commented on Jul 11, 2015

    @s-light
    Contributor

    just if someone is searching the code snippet - it has moved:
    https://github.com/angular/angular.js/blob/master/src/ng/directive/ngModel.js#L822

  8. added 9 commits that reference this issue on Sep 21, 2017
    ba8204a
    77e2466
    7db9943
    29256ce
    dc8a725
    072fea8
    1ee7a7f
    6f08ae8
    e895e27
  9. added a commit that references this issue on Sep 29, 2017
    1451948
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions