Permalink
Browse files

fix($observe): check if the attribute is undefined

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...
1 parent d488a89 commit 531a8de72c439d8ddd064874bf364c00cedabb11 @Puigcerber Puigcerber committed with caitp Oct 21, 2014
Showing with 19 additions and 1 deletion.
  1. +1 −1 src/ng/compile.js
  2. +18 −0 test/ng/directive/inputSpec.js
View
@@ -1072,7 +1072,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
listeners.push(fn);
$rootScope.$evalAsync(function() {
- if (!listeners.$$inter) {
+ if (!listeners.$$inter && attrs.hasOwnProperty(key)) {
@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]);
}
@@ -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;

0 comments on commit 531a8de

Please sign in to comment.