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

Commit 5f8ed63

Browse files
committed
fix(ngModelOptions): work correctly when on the template of replace directives
Previously, in order for `ngModel` and `ngModelOptions` to work correctly together, the latter's pre-linking function should be run before the former's pre-linking function. This was typically what happened, except when `ngModel` was used on an element which also had a `replace` directive, whose template included `ngModelOptions`. In that case, the order was reversed. This commit fixes it by moving the initialization logic of `ngModelOptions` from its pre-linking function to its controller's `$onInit()` lifecycle hook. Fixes #15492 Closes #15493
1 parent e4f3c94 commit 5f8ed63

File tree

2 files changed

+704
-667
lines changed

2 files changed

+704
-667
lines changed

src/ng/directive/ngModelOptions.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,27 @@ defaultModelOptions = new ModelOptions({
331331
*
332332
*/
333333
var ngModelOptionsDirective = function() {
334+
NgModelOptionsController.$inject = ['$attrs', '$scope'];
335+
function NgModelOptionsController($attrs, $scope) {
336+
this.$$attrs = $attrs;
337+
this.$$scope = $scope;
338+
}
339+
NgModelOptionsController.prototype = {
340+
$onInit: function() {
341+
var parentOptions = this.parentCtrl ? this.parentCtrl.$options : defaultModelOptions;
342+
var modelOptionsDefinition = this.$$scope.$eval(this.$$attrs.ngModelOptions);
343+
344+
this.$options = parentOptions.createChild(modelOptionsDefinition);
345+
}
346+
};
347+
334348
return {
335349
restrict: 'A',
336350
// ngModelOptions needs to run before ngModel and input directives
337351
priority: 10,
338-
require: ['ngModelOptions', '?^^ngModelOptions'],
339-
controller: function NgModelOptionsController() {},
340-
link: {
341-
pre: function ngModelOptionsPreLinkFn(scope, element, attrs, ctrls) {
342-
var optionsCtrl = ctrls[0];
343-
var parentOptions = ctrls[1] ? ctrls[1].$options : defaultModelOptions;
344-
optionsCtrl.$options = parentOptions.createChild(scope.$eval(attrs.ngModelOptions));
345-
}
346-
}
352+
require: {parentCtrl: '?^^ngModelOptions'},
353+
bindToController: true,
354+
controller: NgModelOptionsController
347355
};
348356
};
349357

0 commit comments

Comments
 (0)