AngularJSでeval()がどのように機能するかを調べようとしています。
私は、次のしている:
$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);
}
なぜevalを使用していますか?いい考えではない。 – Phix
なぜあなたはこれをやっていますか? – Pytth
@Phix、どのように私は関数にスコープ名を入力し、与えられた変数名でスコープを変更できる動的な関数を作るでしょうか?というのは、変数名の他に、全く同じである同じ関数を使用する複数の$ scope配列を持っているということです。 – PhyCoMath