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

fix(ngModel): allow non-assignable binding when getterSetter is used #8717

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,10 +1578,14 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
pendingDebounce = null,
ctrl = this;

if (!ngModelSet) {
throw minErr('ngModel')('nonassign', "Expression '{0}' is non-assignable. Element: {1}",
$attr.ngModel, startingTag($element));
}
this.$$setOptions = function(options) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about adding this method. Nevermind

ctrl.$options = options;

if (!ngModelSet && (!options || !options.getterSetter)) {
throw minErr('ngModel')('nonassign', "Expression '{0}' is non-assignable. Element: {1}",
$attr.ngModel, startingTag($element));
}
};

/**
* @ngdoc method
Expand Down Expand Up @@ -2165,16 +2169,12 @@ var ngModelDirective = function() {
controller: NgModelController,
link: {
pre: function(scope, element, attr, ctrls) {
// Pass the ng-model-options to the ng-model controller
if (ctrls[2]) {
ctrls[0].$options = ctrls[2].$options;
}

// notify others, especially parent forms

var modelCtrl = ctrls[0],
formCtrl = ctrls[1] || nullFormCtrl;

modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options);

// notify others, especially parent forms
formCtrl.$addControl(modelCtrl);

scope.$on('$destroy', function() {
Expand Down
32 changes: 12 additions & 20 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,6 @@ describe('NgModelController', function() {
});


it('should fail on non-assignable model binding', inject(function($controller) {
var exception;

try {
$controller(NgModelController, {
$scope: null,
$element: jqLite('<input ng-model="1+2">'),
$attrs: {
ngModel: '1+2'
}
});
} catch (e) {
exception = e;
}

expect(exception.message).
toMatch(/^\[ngModel:nonassign\] Expression '1\+2' is non\-assignable\. Element: <input( value="")? ng-model="1\+2">/);
}));


it('should init the properties', function() {
expect(ctrl.$untouched).toBe(true);
expect(ctrl.$touched).toBe(false);
Expand Down Expand Up @@ -1232,6 +1212,18 @@ describe('input', function() {
expect(scope.name).toBe('d');
});

it('should fail on non-assignable model binding if getterSetter is false', function() {
expect(function() {
compileInput('<input type="text" ng-model="accessor(user, \'name\')" />');
}).toThrowMinErr('ngModel', 'nonassign', 'Expression \'accessor(user, \'name\')\' is non-assignable.');
});

it('should not fail on non-assignable model binding if getterSetter is true', function() {
compileInput(
'<input type="text" ng-model="accessor(user, \'name\')" '+
'ng-model-options="{ getterSetter: true }" />');
});

});

it('should allow complex reference binding', function() {
Expand Down