Pass in a function (by reference) a variable in the scope

13,972

You cannot pass a variable by reference in Javascript. However you can pass the complete object and get your values changed.

In your case you can pass the $scope object and then modify the property value. Something like this:

$scope.modifyVar = function (myObj){
    myObj.txtNumberOfChuck = 92;
}

$scope.modifyVar($scope);
Share:
13,972
Joker
Author by

Joker

Updated on June 29, 2022

Comments

  • Joker
    Joker almost 2 years

    There is a way (in angularJS) to pass by reference a variable in the scope in a function (to modify it)? I've tried but the variable change doesn't happen. Here my code

    $scope.modifyVar = function (myVariable){
     myVariable = 92;
    }
    
    $scope.modifyVar($scope.txtNumberOfChuck);