I have a scope variable which has false
value by default.
I want when I pass this variable to a function to modify it's value to true
.
but this can't be done since the passage of params in JavaScript is by value.
this is a simple code of what I'm tryin to do:
myapp.controller('PersonCtrl', function ($scope) {
$scope.step = false;
change($scope.step);
console.log($scope.step);
});
change = function(step){
step = true;
}
jsfiddle:
http://jsfiddle.net/SNF9x/177/
how can I solve this ?
You can pass the object holding the primtive to your change function:
myapp.controller('PersonCtrl', function ($scope) {
$scope.step = false;
change($scope);
console.log($scope.step);
});
change = function(obj){
obj.step = true;
}
You could event pass the name of the variable you want to change:
myapp.controller('PersonCtrl', function ($scope) {
$scope.step = false;
change($scope, 'step');
console.log($scope.step);
});
change = function(obj, prop){
obj[prop] = true;
}
You can't pass variables by reference in JS, but you can pass objects by reference and then modify the properties, like this...
myapp.controller('PersonCtrl', function ($scope) {
$scope.step = false;
change($scope);
console.log($scope.step);
});
change = function($scp){
$scp.step = true;
}
jsfiddle:
http://jsfiddle.net/SNF9x/178/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With