Beginners' angular question. Probably.
Why does
//1
function setValue(target, value) {
target = value;
}
setValue($scope.var1, 25);
not work, but
//2
function setValue(target, value) {
$scope[target] = value;
}
setValue("var1", 25);
does?
The code's inside a controller. I'm trying to make my code more modular but I frown upon passing a variable as a string instead of as a reference. I've tried adding a $scope.$apply()
to the former, as was suggested to me elsewhere, but that's throwing an error here.
Many thanks