From 785ec2632216fdf0a7aa3c2435d41317e3e6998c Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Wed, 18 Feb 2015 10:54:28 -0800 Subject: [PATCH] feat(compiler): make directive bindings optional. Fixes #647 --- .../pipeline/element_binder_builder.js | 30 ++++++++++--------- .../test/core/compiler/integration_spec.js | 14 +++++++++ .../pipeline/element_binder_builder_spec.js | 14 +++++---- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/modules/angular2/src/core/compiler/pipeline/element_binder_builder.js b/modules/angular2/src/core/compiler/pipeline/element_binder_builder.js index 84532dd849..d04c0b1060 100644 --- a/modules/angular2/src/core/compiler/pipeline/element_binder_builder.js +++ b/modules/angular2/src/core/compiler/pipeline/element_binder_builder.js @@ -204,22 +204,24 @@ export class ElementBinderBuilder extends CompileStep { var attributeValue = MapWrapper.get(compileElement.attrs(), elProp); if (isPresent(attributeValue)) { expression = _this._parser.wrapLiteralPrimitive(attributeValue, _this._compilationUnit); - } else { - throw new BaseException("No element binding found for property '" + elProp - + "' which is required by directive '" + stringify(directive.type) + "'"); } } - var len = dirProp.length; - var dirBindingName = dirProp; - var isContentWatch = dirProp[len - 2] === '[' && dirProp[len - 1] === ']'; - if (isContentWatch) dirBindingName = dirProp.substring(0, len - 2); - protoView.bindDirectiveProperty( - directiveIndex, - expression, - dirBindingName, - reflector.setter(dirBindingName), - isContentWatch - ); + + // Bindings are optional, so this binding only needs to be set up if an expression is given. + if (isPresent(expression)) { + var len = dirProp.length; + var dirBindingName = dirProp; + var isContentWatch = dirProp[len - 2] === '[' && dirProp[len - 1] === ']'; + if (isContentWatch) dirBindingName = dirProp.substring(0, len - 2); + + protoView.bindDirectiveProperty( + directiveIndex, + expression, + dirBindingName, + reflector.setter(dirBindingName), + isContentWatch + ); + } }); } } diff --git a/modules/angular2/test/core/compiler/integration_spec.js b/modules/angular2/test/core/compiler/integration_spec.js index 0f88d648a5..e3f25d0e4e 100644 --- a/modules/angular2/test/core/compiler/integration_spec.js +++ b/modules/angular2/test/core/compiler/integration_spec.js @@ -151,6 +151,20 @@ export function main() { }); }); + it('should support directives where a binding attribute is not given', function(done) { + tplResolver.setTemplate(MyComp, + new Template({ + // No attribute "el-prop" specified. + inline: '

', + directives: [MyDir] + })); + + compiler.compile(MyComp).then((pv) => { + createView(pv); + done(); + }); + }); + it('should support template directives via `