javascript - Variable autolink object in angularjs -


i have code in angular

$scope.b = {text: "b"}; $scope.a = {}; $scope.a = $scope.b; $scope.a.text = "a"; console.log($scope.b); // object {text: "a"}; 

it should print object {text: "b"}. don't want 2 variables link (when change a, b should not change)

in javascript, assign object means ref object.

if want make a , b refer different objects. should copy that.

angular provides 2 shortcuts copy:

deep copy angular.copy

$scope.a = {}; //angular.copy(source, destination) angular.copy($scope.b, $scope.a); 

shallow copy angular.extend

$scope.a = {}; //angular.extend(destination, source1, source2 ...) angular.extend($scope.a, $scope.b); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -