snazzybouche snazzybouche - 5 months ago 28
AngularJS Question

How to bind a $scope variable to a normal variable in AngularJS?

In my AngularJS controller, I have a variable

$scope.name
which is assigned to an input's
ngModel
.

I wanted to save the value of
$scope.name
to a normal variable, so I could process it in my controller without changing the value of the input box.

So I did
var name = $scope.name


The issue is, now the two seem to be linked... when I change the value of
name
, the value of
$scope.name
also changes, and that changes the value of the input box.

How can I stop this? How can I assign a
$scope
variable to normal variable, once, without any continued binding?

Thank you!

Answer

You need to use angular.copy()

like:

var name;
$scope.name = 'name';

function copy(){
    name = angular.copy($scope.name);
}

See more