Query
Can a scope variable (specifically an array) be passed by reference to a function triggered by ng-click, and can the value of that variable be manipulated?
Update:
To clarify further, my objective is to avoid accessing $scope in $scope.app.onItemClick
.
Illustration
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'
]
/*
* Tries to assign $scope.app.primarySet to $scope.app.workingSet using references to those 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 refer to this Plunker for more context on this code.
In this code snippet, I expected $scope.app.primarySet
to be set equal to $scope.app.workingSet
when the button is clicked. However, this does not happen. Although within the function's scope, primarySet
is assigned to workingSet
, $scope.app.primarySet
remains unchanged.
Reasoning
I am driven by insights from this response on Stack Overflow. I agree that it would be simpler to test methods that modify scope if direct referencing is avoided. This approach also seems clearer than having a function directly alter the scope.
Prior Investigations
The only source I found related to this topic is this question on Stack Overflow. While similar, the issue here concerns manipulating a string parameter, which supposedly cannot be changed as anticipated with a reference.