0

I have a question regarding the angularjs set value and reset it to default.

Here an example

$scope.defaultValue = {
    a: 1,
    b: 2
}
var dupValue = $scope.defaultValue;

...

function changeValue() {
    dupValue.b = 3;
}

...

After I execute the function changeValue(), I wish to change my dupValue back to defaultValue which I simply just call

dupValue = $scope.defaultValue;

Some how it did't work. Because the defaultValue's element has changed due to the function i executed.

Is there any method to make this work?

1
  • Please post the DOM where these values are bound. Commented Jun 13, 2016 at 12:43

1 Answer 1

5

You'are not copying the defaultValue, but just assigning the reference to it. Therefore when you modify dupValue you also make change to defautValue.

Assign default value using:

dupValue = angular.copy($scope.defaultValue);

See this fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

It work great on my side now, got confused among copying and assigning variable in coding, thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.