mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat(scope.$eval): Allow passing locals to the expression
This commit is contained in:
parent
935c1018da
commit
192ff61f5d
2 changed files with 15 additions and 3 deletions
|
|
@ -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);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue