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 --- src/ng/compile.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/ng/compile.js b/src/ng/compile.js index 5508605e..f39b0935 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -768,13 +768,14 @@ function $CompileProvider($provide) { $element = attrs.$$element; if (newIsolateScopeDirective) { - var LOCAL_REGEXP = /^\s*([@=&])\s*(\w*)\s*$/; + var LOCAL_REGEXP = /^\s*([@=&])(\??)\s*(\w*)\s*$/; var parentScope = scope.$parent || scope; forEach(newIsolateScopeDirective.scope, function(definiton, scopeName) { var match = definiton.match(LOCAL_REGEXP) || [], - attrName = match[2]|| scopeName, + attrName = match[3] || scopeName, + optional = (match[2] == '?'), mode = match[1], // @, =, or & lastValue, parentGet, parentSet; @@ -796,6 +797,9 @@ function $CompileProvider($provide) { } case '=': { + if (optional && !attrs[attrName]) { + return; + } parentGet = $parse(attrs[attrName]); parentSet = parentGet.assign || function() { // reset the change, or we will throw this exception on every $digest -- cgit v1.2.3