Question
Is it possible to pass a scope variable (specifically an array) by reference to a function called using ng-click, and manipulate the value of said variable?
Update:
To be more explicit, my goal here is to avoid having to access $scope in $scope.app.onItemClick
.
Example
Javascript:
(function() {
angular.module('test', [])
angular.module('test').controller('test',['$scope', function ($scope) {
$scope.app = {};
$scope.app.primarySet = [
'test1',
'test2',
'test3'
]
$scope.app.workingSet = [
'test4',
'test5',
'test6'
]
/*
* Attempts to assign $scope.app.primarySet to $scope.app.workingSet using references to those two values.
*/
$scope.app.onItemClick = function (primarySet, workingSet) {
primarySet = workingSet
}
}])
})()
Relevant HTML:
<button type="button" ng-click="app.onItemClick(app.primarySet, app.workingSet)">Update Primary Set</button>
Please see this Plunker for this code in more context.
My expectation for this code is that $scope.app.primarySet
would be set to $scope.app.workingSet
when the button is pressed. Unfortunately, this is not the case. While debugging, in the scope of the function, primarySet
is assigned to workingSet
. However $scope.app.primarySet
is not.
Motivation
My motivation for this is rooted in this SO reply. I share the belief that it will be easier to test methods that manipulate scope if I am not referencing it directly. I also find this more straightforward than having a function manipulate the scope directly.
Previous Research
The only resource that I have come across relating to this is this SO question. While this question comes close, it is different in that the parameter in question is a string, which, if I understand correctly, cannot be modified as a reference could be expected to.