You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
ngModel + ngList binding to array does not update view when model changes #1751
I also encounter this issue when using ng-model in my custom directive. It seems ngModelController using reference comparison leads the $render callback never being called when array is just an old reference but its content changes. angular v1.2.23
functionngeModelCollection(){return{require: 'ngModel',link: function(scope,elem,attr,ctrl){$scope.$watchCollection(attr.ngModel,functionngModelWatch(value,old){if(!Array.isArray(value)||old===value){return;}///copypasta from ngModelWatch()varformatters=ctrl.$formatters,idx=formatters.length;ctrl.$modelValue=value;while(idx--){value=formatters[idx](value);}if(ctrl.$viewValue!==value){ctrl.$viewValue=value;ctrl.$render();}});////endcopypasta}//end link function};}
I've created this workaround via new directive.
It's more performant than @jamie-pate solution , 'cause the $viewValue update is processed only on external $modelValue changes and only one $formatter ngList formatter is run and $modelValue is not reassigned(there is no reason to do that) inside listener function.
angular.module('myApp').directive('emNgList',emNgList);functionemNgList(){return{restrict: 'A',require: 'ngModel',priority: 100,// this has to be eq or gt as ngList.priority to get ngList $formatterlink: postLink};functionpostLink(scope,elem,attrs,ctrl){varngListFormatter=getNgListFormatter(ctrl);varkillMe=scope.$watchCollection(attrs.ngModel,function(modelValue){varnewViewValue=ngListFormatter(modelValue);if(newViewValue!==ctrl.$viewValue){ctrl.$viewValue=newViewValue;ctrl.$render();}});scope.$on('$destroy',function(){killMe();});}functiongetNgListFormatter(ngModelCtrl){varformatters=ngModelCtrl.$formatters;varlastIdx=formatters.length-1;returnformatters[lastIdx];}}
Activity
fix(input): detect changes in array when its values are changed
fix(input,select): equality comparison is used to detect changes
btford commentedon Aug 24, 2013
As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.
Please try the newest versions of Angular (
1.0.8and1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.Thanks!
danilsomsikov commentedon Aug 26, 2013
The issue hasn't been fixed in either version.
See http://jsfiddle.net/fjQcu/8/ for 1.2.0-rc.1 and http://jsfiddle.net/fjQcu/9/ for 1.0.8
fix(rootScope): $watchCollection returns in oldCollection a copy of t…
fix(input): ngList is updated when array model values are changed
fix(rootScope): $watchCollection returns in oldCollection a copy of t…
fix(input): ngList is updated when array model values are changed
lordfriend commentedon Nov 1, 2014
I also encounter this issue when using ng-model in my custom directive. It seems ngModelController using reference comparison leads the $render callback never being called when array is just an old reference but its content changes. angular v1.2.23
jamie-pate commentedon Dec 10, 2014
Workaround:
make a new directive, something like:
(untested, may need tweaking)
kapace commentedon Dec 11, 2014
👍 Just formatting Jamie's workaround
Hotell commentedon Feb 8, 2015
I've created this workaround via new directive.
It's more performant than @jamie-pate solution , 'cause the $viewValue update is processed only on external $modelValue changes and only one $formatter
ngList formatteris run and $modelValue is not reassigned(there is no reason to do that) inside listener function.showcase:
http://plnkr.co/edit/3ExCYEhmhAp1pL21ll3X?p=preview