$ parse vs $ eval?哪一个是最佳实践?

时间:2022-01-16 21:15:28

I know that $parse and $eval both operate on Angular expressions.then why Angular team created these two ?

我知道$ parse和$ eval都在Angular表达式上运行。那么为什么Angular团队创建了这两个?

I checked in angular library, $eval is defined like that:

我检查了角度库,$ eval定义如下:

$eval: function(expr, locals) {
        return $parse(expr)(this, locals);
      }

so what is difference between :

那么有什么区别:

$parse(expr)(context, locals);

and

$eval: function(expr, locals) {
        return $parse(expr)(this, locals);
      }

I want to know which one is best practice ? and when to use these two ?

我想知道哪一个是最佳做法?什么时候使用这两个?

4 个解决方案

#1


30  

As you noticed,

你注意到了,

$parse(expr)($scope, locals)

is exactly equivalent to

完全等同于

$scope.$eval(expr, locals)

But, $parse is more "fundamental" operation than $eval, and so, you could $parse once in one place (for example, in compile function of a directive):

但是,$ parse比$ eval更“基本”操作,因此,你可以在一个地方解析一次(例如,在指令的编译函数中):

var parsedExpr = $parse(tAttrs.p1);

and use repeatedly elsewhere (for example, in a controller function)

并在其他地方重复使用(例如,在控制器功能中)

var childScope1 = $scope.$new();
var childScope2 = $scope.$new();

var r1 = parsedExpr(childScope1);
var r2 = parsedExpr(childScope2);

#2


20  

I think the important difference is that $eval is a scope method that executes an expression on the current scope, while $parse is a (more globally available) service.

我认为重要的区别是$ eval是一个范围方法,它在当前范围内执行表达式,而$ parse是一个(更全局可用的)服务。

So you can say $parse(expr)(context, locals);, with any context, but in the case of $eval the context will be the scope.

所以你可以说$ parse(expr)(context,locals);和任何上下文,但在$ eval的情况下,上下文将是范围。

#3


6  

$eval behind the scenes uses $parse against the current scope.

幕后$ eval使用$ parse来对抗当前范围。

Let us say you have

让我们说你有

$scope.a = 2;
$scope.b = 4;

$parse("a*b") gives you a function which has to be evaluated against the context/object/scope to get the result.

$ parse(“a * b”)为您提供了一个函数,必须根据context / object / scope来计算得到结果。

So now

所以现在

var f = $parse("a*b");

var result = f($scope); we are using the output of $parse which is a function applied against the $scope.

var result = f($ scope);我们使用$ parse的输出,这是一个应用于$ scope的函数。

NOTE:

注意:

  1. $eval always evaluates the expression based on the current scope and returns the result Example: console.log($scope.$eval("a*b")); // 8

    $ eval总是根据当前作用域计算表达式并返回结果示例:console.log($ scope。$ eval(“a * b”)); // 8

  2. $parse just returns the function and does not work on any scope. Example:

    $ parse只返回函数,不适用于任何范围。例:

    var func = $parse("a*b");
    

now func can be applied against

现在可以应用func

  1. any scope

    任何范围

    var result = func($scope);
    console.log(result); // 8
    
  2. object

    目的

    var result1 = func({a:3 , b:3});    
    console.log(result1); // 9
    

So same expression parsed once can be evaluated against any scope or object.

因此,可以针对任何范围或对象评估解析一次的相同表达式。

As said before , for $eval angular uses $parse against the current scope behind the scenes to evaluate the expression and return the result.

如前所述,对于$ eval angular使用$ parse对照幕后的当前作用域来计算表达式并返回结果。

If you want multiple contexts to be executed for the same expression $parse is the best choice.

如果你想为同一个表达式执行多个上下文,$ parse是最好的选择。

#4


0  

“.$eval()” is a simple scope method which takes an expression (and optionally locals) and then executes it against the current scope, using “$parse()”.

“。$ eval()”是一个简单的范围方法,它使用表达式(以及可选的局部变量),然后使用“$ parse()”对当前范围执行它。

The service “$parse()” is where the magic happens. It takes an Angular expression and returns a function, which takes two arguments: “context” and “locals”. The latter is used for overriding variables set in the context. The context is the key part.

服务“$ parse()”是神奇发生的地方。它需要一个Angular表达式并返回一个函数,该函数有两个参数:“context”和“locals”。后者用于覆盖上下文中设置的变量。背景是关键部分。

$scope.foo = 3;
var parseFn = $parse(‘foo = 5’);
parseFn($scope);
$scope.foo; // returns 5

or shorter:

或更短:

$scope.foo = 3;
$scope.$eval(‘foo = 5’);
$scope.foo; // returns 5

#1


30  

As you noticed,

你注意到了,

$parse(expr)($scope, locals)

is exactly equivalent to

完全等同于

$scope.$eval(expr, locals)

But, $parse is more "fundamental" operation than $eval, and so, you could $parse once in one place (for example, in compile function of a directive):

但是,$ parse比$ eval更“基本”操作,因此,你可以在一个地方解析一次(例如,在指令的编译函数中):

var parsedExpr = $parse(tAttrs.p1);

and use repeatedly elsewhere (for example, in a controller function)

并在其他地方重复使用(例如,在控制器功能中)

var childScope1 = $scope.$new();
var childScope2 = $scope.$new();

var r1 = parsedExpr(childScope1);
var r2 = parsedExpr(childScope2);

#2


20  

I think the important difference is that $eval is a scope method that executes an expression on the current scope, while $parse is a (more globally available) service.

我认为重要的区别是$ eval是一个范围方法,它在当前范围内执行表达式,而$ parse是一个(更全局可用的)服务。

So you can say $parse(expr)(context, locals);, with any context, but in the case of $eval the context will be the scope.

所以你可以说$ parse(expr)(context,locals);和任何上下文,但在$ eval的情况下,上下文将是范围。

#3


6  

$eval behind the scenes uses $parse against the current scope.

幕后$ eval使用$ parse来对抗当前范围。

Let us say you have

让我们说你有

$scope.a = 2;
$scope.b = 4;

$parse("a*b") gives you a function which has to be evaluated against the context/object/scope to get the result.

$ parse(“a * b”)为您提供了一个函数,必须根据context / object / scope来计算得到结果。

So now

所以现在

var f = $parse("a*b");

var result = f($scope); we are using the output of $parse which is a function applied against the $scope.

var result = f($ scope);我们使用$ parse的输出,这是一个应用于$ scope的函数。

NOTE:

注意:

  1. $eval always evaluates the expression based on the current scope and returns the result Example: console.log($scope.$eval("a*b")); // 8

    $ eval总是根据当前作用域计算表达式并返回结果示例:console.log($ scope。$ eval(“a * b”)); // 8

  2. $parse just returns the function and does not work on any scope. Example:

    $ parse只返回函数,不适用于任何范围。例:

    var func = $parse("a*b");
    

now func can be applied against

现在可以应用func

  1. any scope

    任何范围

    var result = func($scope);
    console.log(result); // 8
    
  2. object

    目的

    var result1 = func({a:3 , b:3});    
    console.log(result1); // 9
    

So same expression parsed once can be evaluated against any scope or object.

因此,可以针对任何范围或对象评估解析一次的相同表达式。

As said before , for $eval angular uses $parse against the current scope behind the scenes to evaluate the expression and return the result.

如前所述,对于$ eval angular使用$ parse对照幕后的当前作用域来计算表达式并返回结果。

If you want multiple contexts to be executed for the same expression $parse is the best choice.

如果你想为同一个表达式执行多个上下文,$ parse是最好的选择。

#4


0  

“.$eval()” is a simple scope method which takes an expression (and optionally locals) and then executes it against the current scope, using “$parse()”.

“。$ eval()”是一个简单的范围方法,它使用表达式(以及可选的局部变量),然后使用“$ parse()”对当前范围执行它。

The service “$parse()” is where the magic happens. It takes an Angular expression and returns a function, which takes two arguments: “context” and “locals”. The latter is used for overriding variables set in the context. The context is the key part.

服务“$ parse()”是神奇发生的地方。它需要一个Angular表达式并返回一个函数,该函数有两个参数:“context”和“locals”。后者用于覆盖上下文中设置的变量。背景是关键部分。

$scope.foo = 3;
var parseFn = $parse(‘foo = 5’);
parseFn($scope);
$scope.foo; // returns 5

or shorter:

或更短:

$scope.foo = 3;
$scope.$eval(‘foo = 5’);
$scope.foo; // returns 5