From cf17c6af475eace31cf52944afd8e10d3afcf6c0 Mon Sep 17 00:00:00 2001 From: Luis Ramón López Date: Tue, 26 Feb 2013 00:00:47 +0100 Subject: feat($compile): add attribute binding support via ngAttr* Sometimes is not desirable to use interpolation on attributes because the user agent parses them before the interpolation takes place. I.e: The snippet throws three browser errors, one for each attribute. For some attributes, AngularJS fixes that behaviour introducing special directives like ng-href or ng-src. This commit is a more general solution that allows prefixing any attribute with "ng-attr-", "ng:attr:" or "ng_attr_" so it will be set only when the binding is done. The prefix is then removed. Example usage: Closes #1050 Closes #1925 --- test/ng/compileSpec.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'test/ng/compileSpec.js') diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index b9ed3ff9..6f56c6e6 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -2591,4 +2591,41 @@ describe('$compile', function() { }); }); }); + + describe('ngAttr* attribute binding', function() { + + it('should bind after digest but not before', inject(function($compile, $rootScope) { + $rootScope.name = "Misko"; + element = $compile('')($rootScope); + expect(element.attr('test')).toBeUndefined(); + $rootScope.$digest(); + expect(element.attr('test')).toBe('Misko'); + })); + + + it('should work with different prefixes', inject(function($compile, $rootScope) { + $rootScope.name = "Misko"; + element = $compile('')($rootScope); + expect(element.attr('test')).toBeUndefined(); + expect(element.attr('test2')).toBeUndefined(); + expect(element.attr('test2')).toBeUndefined(); + $rootScope.$digest(); + expect(element.attr('test')).toBe('Misko'); + expect(element.attr('test2')).toBe('Misko'); + expect(element.attr('test3')).toBe('Misko'); + })); + + + it('should work if they are prefixed with x- or data-', inject(function($compile, $rootScope) { + $rootScope.name = "Misko"; + element = $compile('')($rootScope); + expect(element.attr('test2')).toBeUndefined(); + expect(element.attr('test3')).toBeUndefined(); + expect(element.attr('test4')).toBeUndefined(); + $rootScope.$digest(); + expect(element.attr('test2')).toBe('Misko'); + expect(element.attr('test3')).toBe('Misko'); + expect(element.attr('test4')).toBe('Misko'); + })); + }); }); -- cgit v1.2.3