如何在 angularjs 中使用 $scope.eval() 来设置 $scope 变量
How to use $scope.eval() in angularjs to set $scope variable
我试图弄清楚 eval() 在 AngularJS 中是如何工作的,但我似乎无法理解它。
我有以下内容:
$scope.salaryRate, $scope.priceRate = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
}
$scope.addRate = function (variable) {
eval('$scope.' + variable).push({
'number': eval('$scope.' + variable + 'Rate').number,
'type': eval('$scope.' + variable + 'Rate').type,
'days': {
'days': eval('$scope.' + variable + 'SelectedDay'),
'daynames': displayDayNames($scope.dayNames, eval('$scope.' + variable + 'SelectedDay'))},
'from': eval('$scope.' + variable + 'Rate').from,
'to': eval('$scope.' + variable + 'Rate').to,
'rate': eval('$scope.' + variable + 'Rate').rate,
'id': eval(variable + 'Number')
});
eval('$scope.' + variable + 'Rate') = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
};
}
出于某种原因,当我将值推送到变量数组时,eval 工作正常。但是由于某种原因 eval('$scope.' + variable + 'Rate') =
不起作用,我收到错误 ReferenceError: Invalid left-hand side in assignment
。
我正在尝试使我的函数动态化,以便我可以将它用于多个 $scope
。如何使用 AngularJS?
解决此问题
此时也刚好失败
function findRowIndex(variable, id){
eval('var ' + variable + 'Rows') = eval('$scope.' + variable);
}
你为什么不直接使用 bracket notation? https://jsfiddle.net/duoxoq8j/2/
$scope.addRate = function (variable) {
$scope[variable].push({
...
});
$scope[variable + "Rate"] = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
};
}
这只是基本的 javascript 功能,与 Angular 或特定范围无关。
我试图弄清楚 eval() 在 AngularJS 中是如何工作的,但我似乎无法理解它。
我有以下内容:
$scope.salaryRate, $scope.priceRate = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
}
$scope.addRate = function (variable) {
eval('$scope.' + variable).push({
'number': eval('$scope.' + variable + 'Rate').number,
'type': eval('$scope.' + variable + 'Rate').type,
'days': {
'days': eval('$scope.' + variable + 'SelectedDay'),
'daynames': displayDayNames($scope.dayNames, eval('$scope.' + variable + 'SelectedDay'))},
'from': eval('$scope.' + variable + 'Rate').from,
'to': eval('$scope.' + variable + 'Rate').to,
'rate': eval('$scope.' + variable + 'Rate').rate,
'id': eval(variable + 'Number')
});
eval('$scope.' + variable + 'Rate') = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
};
}
出于某种原因,当我将值推送到变量数组时,eval 工作正常。但是由于某种原因 eval('$scope.' + variable + 'Rate') =
不起作用,我收到错误 ReferenceError: Invalid left-hand side in assignment
。
我正在尝试使我的函数动态化,以便我可以将它用于多个 $scope
。如何使用 AngularJS?
此时也刚好失败
function findRowIndex(variable, id){
eval('var ' + variable + 'Rows') = eval('$scope.' + variable);
}
你为什么不直接使用 bracket notation? https://jsfiddle.net/duoxoq8j/2/
$scope.addRate = function (variable) {
$scope[variable].push({
...
});
$scope[variable + "Rate"] = {
number: undefined,
type: undefined,
from: undefined,
to: undefined,
rate: undefined
};
}
这只是基本的 javascript 功能,与 Angular 或特定范围无关。