Material: feat(contact-chips): add support for ng-change

Created on 22 Jul 2015  路  12Comments  路  Source: angular/material

Hi guys, here's a codepen showing the issue.

Basically, to trigger a function when the <md-contact-chips> updates, we (seemingly) have to use $scope.$watch, which I'm trying to avoid thorough the codebase. Can we have it working (or a similar attribute such as md-contact-change)?

minor external contributor fixed inconvenient enhancement

Most helpful comment

Try with $scope.$watchCollection or if not pass in a 3rd parameter as true:

$scope.$watchCollection('whatever', fn)
//or
$scope.$watch('whatever', fn, true);

All 12 comments

Try with $scope.$watchCollection or if not pass in a 3rd parameter as true:

$scope.$watchCollection('whatever', fn)
//or
$scope.$watch('whatever', fn, true);

Thanks for the heads up! But I鈥檇 really like it as an attribute.

Well... You can always do a directive. The thing is that the actual ng-change is based on the view change and not on the model. Im not sure what is really the difference but this is the source code from ng-change :

var ngChangeDirective = valueFn({
  restrict: 'A',
  require: 'ngModel',
  link: function(scope, element, attr, ctrl) {
    ctrl.$viewChangeListeners.push(function() {
      scope.$eval(attr.ngChange);
    });
  }
});

You can always do something like this:

.directive('hardChange', function(){
    return {
        restrict: A,
        require: 'ngModel',
        link: function(scope, elem, attr, ctrl){
            scope.$watchCollection(function(){
                 return ctrl;
            }, function(newVal, oldVal){
              if(newVal === oldVal) return;
              scope.$eval(attr.hardChange)
            });
       }
    };
});

Haven't tested it but if it doesn't work, this will for sure (hopefully :smile: ):

.directive('hardChange', function(){
    return {
        restrict: A,
        require: 'ngModel',
        link: function(scope, elem, attr, ctrl){
            scope.$watch(function(){
                 return ctrl;
            }, function(newVal, oldVal){
              if(newVal === oldVal) return;
              scope.$eval(attr.hardChange)
            }, true);
       }
    };
});

Usage:
<ANY hard-change="fn()"></ANY>

Yeah sure but that looks to me like a feature Material could offer by itself!

Do a fork and do a PR yourself if you want...

What we need to do here, is update the $viewValue of the ngModelCtrl on md-chips when the collection changes. This should not be too hard, and should make ng-change work for you.

Hi peeps. I'm a bit of a noob when it comes to Github and how all this works. I've read this as it's not released yet and 1.1.0 is the target release for this functionality?... That correct?

any chance of this ever being done?

Has this been fixed? we need this in order to subscribe to ngModel $viewChangeListeners.

Why was this closed? This issue isn't fixed.

This is still an issue.

Reopening this because the original PR was closed (not merged) and there has been a significant number of requests to reopen the issue here and in https://github.com/angular/material/issues/3580.

Does this comment provide an appropriate way of detecting changes via md-on-add and md-on-remove?

If those aren't sufficient and someone wants to open a PR with a fix, we'll take a look at it and try to get it merged. Before working on the PR though, please submit an updated CodePen that reproduces this issue with AngularJS 1.6.6 and AngularJS Material 1.1.7.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robertmesserle picture robertmesserle  路  3Comments

bobber205 picture bobber205  路  3Comments

chriseyhorn picture chriseyhorn  路  3Comments

kasajian picture kasajian  路  3Comments

nikhildev picture nikhildev  路  3Comments