Reputation: 944
How can I update the value of a variable that's sent to a function, from within that same function?
HTML:
Message is: {{ msg }}
<button ng-click="updateVar(msg)">Update</button>
AngularJS:
$scope.msg = 'Init';
$scope.updateVar = function(varToUpdate) {
varToUpdate = 'Var was updated!';
};
I can't do $scope.msg = 'Var was updated!';
, since in the function I'm writing, I have many variables to potentially update, and I don't want to write different cases for every single variable.
Thank you!
Upvotes: 0
Views: 22
Reputation: 691973
updateVar(msg)
passes the value of $scope.msg
to the function.
You want to pass the variable name (or rather, the $scope property name): updateVar('msg')
And your function can then use the variable name to modify its value:
$scope.updateVar = function(varToUpdate) {
$scope[varToUpdate] = 'Var was updated!';
};
Upvotes: 1