Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Possible error with ngModel.$render with null/undefined values #967

Closed
ro0sterjam opened this issue May 27, 2015 · 1 comment
Closed

Possible error with ngModel.$render with null/undefined values #967

ro0sterjam opened this issue May 27, 2015 · 1 comment

Comments

@ro0sterjam
Copy link

I was having an issue with a multiple select item when the provided model was undefined and noticed that ngModel.$render first checks if ngModel.$viewValue is null, and if so, set $select.selected to an empty array.

However, immediately after, $select.selected is set to ngModel.$viewValue again, causing it to become undefined again. I suppose a fix would be to set ngModel.$viewValue = [] rather than $select.selected = [].

Original Code:

  ngModel.$render = function() {
    // Make sure that model value is array
    if(!angular.isArray(ngModel.$viewValue)){
      // Have tolerance for null or undefined values
      if(angular.isUndefined(ngModel.$viewValue) || ngModel.$viewValue === null){
        $select.selected = [];
      } else {
        throw uiSelectMinErr('multiarr', "Expected model value to be array but got '{0}'", ngModel.$viewValue);
      }
    }
    $select.selected = ngModel.$viewValue;
    scope.$evalAsync(); //To force $digest
  };

Possible fix:

  ngModel.$render = function() {
    // Make sure that model value is array
    if(!angular.isArray(ngModel.$viewValue)){
      // Have tolerance for null or undefined values
      if(angular.isUndefined(ngModel.$viewValue) || ngModel.$viewValue === null){
        ngModel.$viewValue = [];
      } else {
        throw uiSelectMinErr('multiarr', "Expected model value to be array but got '{0}'", ngModel.$viewValue);
      }
    }
    $select.selected = ngModel.$viewValue;
    scope.$evalAsync(); //To force $digest
  };
@wesleycho
Copy link
Contributor

Can you post a reproduction in Plunker so it is clearer what is meant?

user378230 added a commit to user378230/ui-select that referenced this issue Jul 7, 2016
user378230 added a commit to user378230/ui-select that referenced this issue Jul 7, 2016
Previously view value was checked and corrected if undefined, but then
overwritten with the invalid value again.

This is a minor commit to ensure the updated value isn't overwritten
again.

Originally authored by homerjam

Closes angular-ui#967 and angular-ui#1686
Supersedes angular-ui#1035 angular-ui#1529 angular-ui#1687
user378230 added a commit that referenced this issue Jul 7, 2016
Previously view value was checked and corrected if undefined, but then
overwritten with the invalid value again.

This is a minor commit to ensure the updated value isn't overwritten
again.

Originally authored by homerjam

Closes #967 and #1686
Supersedes #1035 #1529 #1687
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants