Does AngularJS have any function or method similar to eval
?
I need to get the value of a previously-defined variable, e.g.:
data.mess_5 = "Hello"
c = 5
x = eval( "data.mess_" + c )
Does AngularJS have any function or method similar to eval
?
I need to get the value of a previously-defined variable, e.g.:
data.mess_5 = "Hello"
c = 5
x = eval( "data.mess_" + c )
Angularjs is javascript, so you can use eval, assuming your var is on the global scope. However, in this particular case (and most other cases), eval is the wrong tool for the job.
data.mess_5 = "Hello"
c = 5
x = data['mess_' + c]
Check the $parse function (taken from Setting dynamic scope variables in AngularJs - scope.<some_string>)
var the_string = 'life.meaning';
// Get the model
var model = $parse(the_string);
// Assigns a value to it
model.assign($scope, 42);
// Apply it to the scope
$scope.$apply();
console.log($scope.life.meaning); // logs 42