From ac899d0da59157fa1c6429510791b6c3103d9401 Mon Sep 17 00:00:00 2001 From: Luis Ramón López Date: Sat, 26 Jan 2013 20:15:06 +0100 Subject: feat($compile): '=?' makes '=' binding optional If you bind using '=' to a non-existant parent property, the compiler will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception, which is right because the model doesn't exist. This enhancement allow to specify that a binding is optional so it won't complain if the parent property is not defined. In order to mantain backward compability, the new behaviour must be specified using '=?' instead of '='. The local property will be undefined is these cases. Closes #909 Closes #1435 --- test/ng/compileSpec.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'test') diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 9d0acc22..ae1eb000 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -1857,6 +1857,9 @@ describe('$compile', function() { ref: '=', refAlias: '= ref', reference: '=', + optref: '=?', + optrefAlias: '=? optref', + optreference: '=?', expr: '&', exprAlias: '&expr' }, @@ -1993,6 +1996,33 @@ describe('$compile', function() { }); + describe('optional object reference', function() { + it('should update local when origin changes', inject(function() { + compile('
'); + expect(componentScope.optRef).toBe(undefined); + expect(componentScope.optRefAlias).toBe(componentScope.optRef); + + $rootScope.name = 'misko'; + $rootScope.$apply(); + expect(componentScope.optref).toBe($rootScope.name); + expect(componentScope.optrefAlias).toBe($rootScope.name); + + $rootScope.name = {}; + $rootScope.$apply(); + expect(componentScope.optref).toBe($rootScope.name); + expect(componentScope.optrefAlias).toBe($rootScope.name); + })); + + it('should not throw exception when reference does not exist', inject(function() { + compile('
'); + + expect(componentScope.optref).toBe(undefined); + expect(componentScope.optrefAlias).toBe(undefined); + expect(componentScope.optreference).toBe(undefined); + })); + }); + + describe('executable expression', function() { it('should allow expression execution with locals', inject(function() { compile('
'); -- cgit v1.2.3