0

Beginners' angular question. Probably.

Why does

//1
function setValue(target, value) {
    target = value;
}
setValue($scope.var1, 25);

not work, but

//2
function setValue(target, value) {
    $scope[target] = value;
}
setValue("var1", 25);

does?

The code's inside a controller. I'm trying to make my code more modular but I frown upon passing a variable as a string instead of as a reference. I've tried adding a $scope.$apply() to the former, as was suggested to me elsewhere, but that's throwing an error here.

Many thanks

ElRudi
  • 2,122
  • 2
  • 18
  • 33
  • 1
    in javascript objects are passed by value and not by reference.The target and value parameters are local variables to the function and changes will not reflect after the function call is completed(if that is what you are expecting to happen). – Prabhu Murthy Aug 17 '14 at 13:44
  • That _is_ what I'm expecting to happen :) I understand now, all I'm doing in //1 is that I'm reassigning the variable `target`, which initially has the value of whatever `$scope.var1` is, to the value `25`, but this does not change the value of `$scope.var1` itself. Thanks for clearing that up. – ElRudi Aug 17 '14 at 14:09

1 Answers1

0

Angularjs updates it's view when the value of $scope changes.

So according to your first code block

function setValue(target, value) {
    target = value; 
}
setValue($scope.var1, 25);

Because $scope.var1 is a string so here it will be passed by value, & when you are updating the target variable $scope remains same, that's why it's not being changed.

In your second codeblock

function setValue(target, value) {
    $scope[target] = value;
}
setValue("var1", 25);

Here you are directly changing $scope so view is being updated.

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • Ah I see. So I cannot pass a reference to a variable or object for it to be changed elsewhere; only its properties can be changed. I actually found a good explanation here: http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language So, if I wanted to change the value of `$scope.var1`, I can either (a) write a function that returns that value and use it as such: `$scope.var1 = setValue(...)`, (b) hard-code the change in the function: `function setValueVar1(value) {$scope.var1 = value;}`, or (c) do as my second function and pass the name...correct? – ElRudi Aug 17 '14 at 14:02
  • you are not passing a reference you are passing a string & string doesn't passed by reference @ElRudi – Mritunjay Aug 17 '14 at 14:03
  • One thing though - you write "because `$scope.var1` is a string" -- but isn't that irrelevant? Actually we don't know what `$scope.var1` is; it could be an object, and the behaviour would be the same, right? – ElRudi Aug 17 '14 at 14:12
  • @ElRudi it's not if it's a Object it will be passed by reference, So there things can surprise. – Mritunjay Aug 17 '14 at 14:15
  • If it's an Object I can only make 'lasting' changes if I use something like `target.prop1 = ...` to change the Object's properties -- I could never _replace_ the whole object with `target = ...`, as far as I understand. Thanks for your answer! – ElRudi Aug 17 '14 at 14:41