4

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 ?

3
  • Why don't you declare a global variable? Commented Apr 20, 2016 at 14:17
  • pass the $scope instead of $scope.step if possible, else just set $scope.step to the return value of the function. Commented Apr 20, 2016 at 14:18
  • @HarishKumar In any non-trivial application, global variables quickly become cumbersome and prone to conflicts Commented Apr 20, 2016 at 14:20

3 Answers 3

5

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

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/

5 Comments

I would recommend against starting the parameter name with a $.
I would not, since the original variable uses a $ prefix, but it's neither right nor wrong and just preference.
I'm thinking that it would cause confusion; generally (or at least in my experience) $var means it's global. If it isn't, in a larger method it may end up being confusing.
As I said, it's a preference. I personally use _ to prefix global variables, and $ if it's a jQuery object. In this question I simply left it with the prefix it already had. This really isn't a point for discussion though, as it's nothing more than a choice.
Oh, I must have missed those last three words. Fair enough.
1

In your OP you try to change the value of a primitive type:

var o = {
  b: true
};
changeValue(o.b);//pass a primitive type to the function
console.log(o.b);//print out true because is primitive type and all primitive types pass by value

function changeValue(b) {
  b = false;
}

One solution will be to pass the object $scope to the function:

var myapp = angular.module('myapp', []);

myapp.controller('PersonCtrl', function($scope) {
  $scope.step = false;
  change($scope);
  console.log($scope.step);//prints out true
});

change = function(obj) {
  obj.step = true;
}

fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.