diff --git a/src/javascripts/ng-admin/Crud/field/maChoiceField.js b/src/javascripts/ng-admin/Crud/field/maChoiceField.js index ecbd7ec8..a0caffc8 100644 --- a/src/javascripts/ng-admin/Crud/field/maChoiceField.js +++ b/src/javascripts/ng-admin/Crud/field/maChoiceField.js @@ -37,25 +37,33 @@ export default function maChoiceField($compile) { itemsFilter = ''; } - var choices = (typeof scope.choices == 'function' && scope.choices()) ? scope.choices() : (field.choices ? field.choices() : []); + const choices = scope.choices ? scope.choices : field.choices ? field.choices() : []; + let choicesFactory; + + if (typeof choices === 'function' && choices(scope.entry)) { + choicesFactory = choices; + scope.choices = choicesFactory(scope.entry); + } else { + scope.choices = choices ? choices : []; + } var template = ` {{ $select.selected.label | translate }} - + {{ item.label | translate }} `; // as choices may be a function depending of another entry field, we need to watch the whole entry scope.$watch('entry', (newEntry, oldEntry) => { - if (typeof(choices) !== 'function') { - scope.choices = choices; + if (!choicesFactory) { return; } - scope.choices = choices(newEntry); - if (!angular.equals(scope.choices, choices(oldEntry))) { + const oldChoices = scope.choices; + scope.choices = choicesFactory(newEntry); + if (!angular.equals(scope.choices, oldChoices)) { scope.value = null; } }, true);