Skip to content

Commit

Permalink
Expression Binding
Browse files Browse the repository at this point in the history
  • Loading branch information
teropa committed Dec 24, 2014
1 parent 9091390 commit ff935c2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function isBooleanAttribute(node, attrName) {
function parseIsolateBindings(scope) {
var bindings = {};
_.forEach(scope, function(definition, scopeName) {
var match = definition.match(/\s*(@|=(\*?))\s*(\w*)\s*/);
var match = definition.match(/\s*([@&]|=(\*?))\s*(\w*)\s*/);
bindings[scopeName] = {
mode: match[1][0],
collection: match[2] === '*',
Expand Down Expand Up @@ -403,6 +403,12 @@ function $CompileProvider($provide) {
scope.$watch(parentValueWatch);
}
break;
case '&':
var parentExpr = $parse(attrs[attrName]);
isolateScope[scopeName] = function(locals) {
return parentExpr(scope, locals);
};
break;
}
});
}
Expand Down
46 changes: 46 additions & 0 deletions test/compile_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,52 @@ describe('$compile', function() {
});
});

it('allows binding an invokable expression on the parent scope', function() {
var givenScope;
var injector = makeInjectorWithDirectives('myDirective', function() {
return {
scope: {
myExpr: '&'
},
link: function(scope) {
givenScope = scope;
}
};
});
injector.invoke(function($compile, $rootScope) {
$rootScope.parentFunction = function() {
return 42;
};
var el = $('<div my-directive my-expr="parentFunction() + 1"></div>');
$compile(el)($rootScope);
expect(givenScope.myExpr()).toBe(43);
});
});

it('allows passing arguments to parent scope expression', function() {
var givenScope;
var injector = makeInjectorWithDirectives('myDirective', function() {
return {
scope: {
myExpr: '&'
},
link: function(scope) {
givenScope = scope;
}
};
});
injector.invoke(function($compile, $rootScope) {
var gotArg;
$rootScope.parentFunction = function(arg) {
gotArg = arg;
};
var el = $('<div my-directive my-expr="parentFunction(argFromChild)"></div>');
$compile(el)($rootScope);
givenScope.myExpr({argFromChild: 42});
expect(gotArg).toBe(42);
});
});

});

});

0 comments on commit ff935c2

Please sign in to comment.