Skip to content

Commit ff935c2

Browse files
committed
Expression Binding
1 parent 9091390 commit ff935c2

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/compile.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function isBooleanAttribute(node, attrName) {
3636
function parseIsolateBindings(scope) {
3737
var bindings = {};
3838
_.forEach(scope, function(definition, scopeName) {
39-
var match = definition.match(/\s*(@|=(\*?))\s*(\w*)\s*/);
39+
var match = definition.match(/\s*([@&]|=(\*?))\s*(\w*)\s*/);
4040
bindings[scopeName] = {
4141
mode: match[1][0],
4242
collection: match[2] === '*',
@@ -403,6 +403,12 @@ function $CompileProvider($provide) {
403403
scope.$watch(parentValueWatch);
404404
}
405405
break;
406+
case '&':
407+
var parentExpr = $parse(attrs[attrName]);
408+
isolateScope[scopeName] = function(locals) {
409+
return parentExpr(scope, locals);
410+
};
411+
break;
406412
}
407413
});
408414
}

test/compile_spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,52 @@ describe('$compile', function() {
13921392
});
13931393
});
13941394

1395+
it('allows binding an invokable expression on the parent scope', function() {
1396+
var givenScope;
1397+
var injector = makeInjectorWithDirectives('myDirective', function() {
1398+
return {
1399+
scope: {
1400+
myExpr: '&'
1401+
},
1402+
link: function(scope) {
1403+
givenScope = scope;
1404+
}
1405+
};
1406+
});
1407+
injector.invoke(function($compile, $rootScope) {
1408+
$rootScope.parentFunction = function() {
1409+
return 42;
1410+
};
1411+
var el = $('<div my-directive my-expr="parentFunction() + 1"></div>');
1412+
$compile(el)($rootScope);
1413+
expect(givenScope.myExpr()).toBe(43);
1414+
});
1415+
});
1416+
1417+
it('allows passing arguments to parent scope expression', function() {
1418+
var givenScope;
1419+
var injector = makeInjectorWithDirectives('myDirective', function() {
1420+
return {
1421+
scope: {
1422+
myExpr: '&'
1423+
},
1424+
link: function(scope) {
1425+
givenScope = scope;
1426+
}
1427+
};
1428+
});
1429+
injector.invoke(function($compile, $rootScope) {
1430+
var gotArg;
1431+
$rootScope.parentFunction = function(arg) {
1432+
gotArg = arg;
1433+
};
1434+
var el = $('<div my-directive my-expr="parentFunction(argFromChild)"></div>');
1435+
$compile(el)($rootScope);
1436+
givenScope.myExpr({argFromChild: 42});
1437+
expect(gotArg).toBe(42);
1438+
});
1439+
});
1440+
13951441
});
13961442

13971443
});

0 commit comments

Comments
 (0)