I would like to try and have a generic function in my controller that I can use from my view to tell a specific variable within the scope to change to a specific value.
Simplified example
Controller
$scope = {
hair: {
color: 'blue',
length: '2cm'
},
mouth: 'wide',
arms: [
{
fingers: 5,
color: 'brown'
},
{
fingers: 4,
color: 'white'
}
]
}
$scope.updateVariable = function(scopeVariable, value){
scopeVariable = value;
}
View
<a ng-click="updateVariable(hair.color, 'red');">red hair</a>
<a ng-click="updateVariable(hair.length, '5cm');">increase hair length</a>
<a ng-click="updateVariable(mouth, 'narrow');">narrow mouth</a>
<a ng-click="updateVariable(arms[0].fingers, 4);">4 fingers</a>
It seems only the value of the variable is passed on to the function but not the reference. Is there a way for me to get the reference to the scope variable instead of its value from a function parameter? Furthermore can that be done dynamically? And by this I mean I need to pass the "path" to where this variable is located in the scope.
I am aware that this can be done with independent setter functions (i.e.: setMouth('narrow')) but let's assume for the sake of this exercise we do not know ahead of time the structure of the scope in the controller but only in the view and because of that we need a generic function that can deal with property.