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

Commit

Permalink
fix($observe): check if the attribute is undefined
Browse files Browse the repository at this point in the history
Check if the attribute is undefined before manually applying the function because if not an
undefined property is added to the scope of the form controller when the input control does not
have a name.

Closes #9707
Closes #9720
  • Loading branch information
Puigcerber authored and caitp committed Oct 22, 2014
1 parent d488a89 commit 531a8de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

listeners.push(fn);
$rootScope.$evalAsync(function() {
if (!listeners.$$inter) {
if (!listeners.$$inter && attrs.hasOwnProperty(key)) {

This comment has been minimized.

Copy link
@dlongley

dlongley Nov 10, 2014

Contributor

Note: If anyone was using attrs.$observe() with an optional attribute (and perhaps using it to set a default value), the handler will no longer execute, which may break your app. If you need the handler to execute, I recommend setting a default value (attrs.foo = attrs.foo || 'foo') prior to calling $observe.

// no one registered attribute interpolation function, so lets call it manually
fn(attrs[key]);
}
Expand Down
18 changes: 18 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,24 @@ describe('input', function() {
expect(scope.name).toEqual('adam');
});


it('should not add the property to the scope if name is unspecified', function() {
inputElm = jqLite('<input type="text" ng-model="name">');
formElm = jqLite('<form name="form"></form>');
formElm.append(inputElm);
$compile(formElm)(scope);

spyOn(scope.form, '$addControl').andCallThrough();
spyOn(scope.form, '$$renameControl').andCallThrough();

scope.$digest();

expect(scope.form['undefined']).toBeUndefined();
expect(scope.form.$addControl).not.toHaveBeenCalled();
expect(scope.form.$$renameControl).not.toHaveBeenCalled();
});


describe('compositionevents', function() {
it('should not update the model between "compositionstart" and "compositionend" on non android', inject(function($sniffer) {
$sniffer.android = false;
Expand Down

0 comments on commit 531a8de

Please sign in to comment.