Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(scope.$eval): Allow passing locals to the expression
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Mar 19, 2012
1 parent 935c101 commit 192ff61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/service/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,13 @@ function $RootScopeProvider(){
* @param {(string|function())=} expression An angular expression to be executed.
*
* - `string`: execute using the rules as defined in {@link guide/dev_guide.expressions expression}.
* - `function(scope)`: execute the function with the current `scope` parameter.
* - `function(scope, locals)`: execute the function with the current `scope` parameter.
* @param {Object=} locals Hash object of local variables for the expression.
*
* @returns {*} The result of evaluating the expression.
*/
$eval: function(expr) {
return $parse(expr)(this);
$eval: function(expr, locals) {
return $parse(expr)(this, locals);
},

/**
Expand Down
11 changes: 11 additions & 0 deletions test/service/scopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,19 @@ describe('Scope', function() {
$rootScope.$eval(function(self) {self.b=2;});
expect($rootScope.b).toEqual(2);
}));


it('should allow passing locals to the expression', inject(function($rootScope) {
expect($rootScope.$eval('a+1', {a: 2})).toBe(3);

$rootScope.$eval(function(scope, locals) {
scope.c = locals.b + 4;
}, {b: 3});
expect($rootScope.c).toBe(7);
}));
});


describe('$evalAsync', function() {

it('should run callback before $watch', inject(function($rootScope) {
Expand Down

0 comments on commit 192ff61

Please sign in to comment.