简体   繁体   中英

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!

You need to use angular.copy()

like:

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

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

See more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM