$eval()和$parse在AngularJS中的区别是什么?

时间:2021-08-25 13:32:54

In some cases AngularJs documentation suggests we use eval() during the $digest cycle and in some case we are suggested to use parse() during $digest cycle? Can anyone give a good example when in $digest cycle we should use $eval() and $parse(), respectively?

在某些情况下,AngularJs文档建议在$digest周期中使用eval(),在某些情况下建议在$digest周期中使用parse() ?在$digest循环中,我们应该分别使用$eval()和$parse(),谁能给出一个好的示例吗?

2 个解决方案

#1


2  

$parse takes an angular expression and returns a function that represents that expression.

$parse接受一个角度表达式并返回一个表示该表达式的函数。

$eval takes an angular expression, evaluates it and returns the actual result of that expression.

$eval获取一个角表达式,对其进行计算并返回该表达式的实际结果。

You can think of it like this (this is pseudo-example code to show what I mean, this is not how $parse actually works):

你可以这样想(这是伪示例代码来说明我的意思,这不是$parse的实际工作方式):

function $parse(expression) {
    return function() {
        return $eval(expression);
    }
}

#2


0  

$parse is used to create a function based on defined objects / expressions, e.g:

$parse用于根据定义的对象/表达式创建一个函数,例如:

var getter = $parse('user.name');
var setter = getter.assign;
var context = {user:{name:'angular'}};
var locals = {user:{name:'local'}};

expect(getter(context)).toEqual('angular');
setter(context, 'newValue');
expect(context.user.name).toEqual('newValue');
expect(getter(context, locals)).toEqual('local');

Simply with $parse you're able to create an OO Object with Getter, Setter, details are given at https://docs.angularjs.org/api/ng/service/$parse and Source: https://github.com/angular/angular.js/blob/master/src/ng/parse.js#L1678.

只需使用$parse,您就可以创建一个具有Getter、Setter和详细信息的OO对象,这些内容将在https://docs.angularjs.org/api/ng/service/$parse和Source中给出:https://github.com/angular/angular/angular/master/src/ng/parse.js #L1678。

$eval is for retrieving a result based on an expression:

$eval用于基于表达式检索结果:

var scope = ng.$rootScope.Scope();
scope.a = 1;
scope.b = 2;

expect(scope.$eval('a+b')).toEqual(3);
expect(scope.$eval(function(scope){ return scope.a + scope.b; })).toEqual(3);

The interesting fact is that $eval is using $parse to create an function $eval implementation:

有趣的是,$eval使用$parse来创建一个函数:$eval实现:

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

This is related to the fact, if I understand the $parse code right, that $parse checks what kind of expression and context (scope) you have. Basically, $eval is a shorthand use for something like (instead of doing it each time for your $scope Object in the controller for example):

这与以下事实有关,如果我理解了$parse代码,$parse就检查了您拥有的表达式和上下文(作用域)。基本上,$eval是类似于(而不是在控制器中的$scope对象每次都这么做)的一种简写用法:

var getter = $parse("a");
var setter = getter.assign;
scope.a = 1;

expect(getter(scope)).toEqual(1);

#1


2  

$parse takes an angular expression and returns a function that represents that expression.

$parse接受一个角度表达式并返回一个表示该表达式的函数。

$eval takes an angular expression, evaluates it and returns the actual result of that expression.

$eval获取一个角表达式,对其进行计算并返回该表达式的实际结果。

You can think of it like this (this is pseudo-example code to show what I mean, this is not how $parse actually works):

你可以这样想(这是伪示例代码来说明我的意思,这不是$parse的实际工作方式):

function $parse(expression) {
    return function() {
        return $eval(expression);
    }
}

#2


0  

$parse is used to create a function based on defined objects / expressions, e.g:

$parse用于根据定义的对象/表达式创建一个函数,例如:

var getter = $parse('user.name');
var setter = getter.assign;
var context = {user:{name:'angular'}};
var locals = {user:{name:'local'}};

expect(getter(context)).toEqual('angular');
setter(context, 'newValue');
expect(context.user.name).toEqual('newValue');
expect(getter(context, locals)).toEqual('local');

Simply with $parse you're able to create an OO Object with Getter, Setter, details are given at https://docs.angularjs.org/api/ng/service/$parse and Source: https://github.com/angular/angular.js/blob/master/src/ng/parse.js#L1678.

只需使用$parse,您就可以创建一个具有Getter、Setter和详细信息的OO对象,这些内容将在https://docs.angularjs.org/api/ng/service/$parse和Source中给出:https://github.com/angular/angular/angular/master/src/ng/parse.js #L1678。

$eval is for retrieving a result based on an expression:

$eval用于基于表达式检索结果:

var scope = ng.$rootScope.Scope();
scope.a = 1;
scope.b = 2;

expect(scope.$eval('a+b')).toEqual(3);
expect(scope.$eval(function(scope){ return scope.a + scope.b; })).toEqual(3);

The interesting fact is that $eval is using $parse to create an function $eval implementation:

有趣的是,$eval使用$parse来创建一个函数:$eval实现:

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

This is related to the fact, if I understand the $parse code right, that $parse checks what kind of expression and context (scope) you have. Basically, $eval is a shorthand use for something like (instead of doing it each time for your $scope Object in the controller for example):

这与以下事实有关,如果我理解了$parse代码,$parse就检查了您拥有的表达式和上下文(作用域)。基本上,$eval是类似于(而不是在控制器中的$scope对象每次都这么做)的一种简写用法:

var getter = $parse("a");
var setter = getter.assign;
scope.a = 1;

expect(getter(scope)).toEqual(1);