2

I'm new in angular js and bumped ( I think ) in to simple problem. Sorry if I dublicated question, but I can't find any related topics.

I need to set $scope variable without affecting each other, but then I'm setting them, both are updated with the same results:

var defaultFilterValues = {
    show_limit: 20,
    purpose: {}
};

$scope.filter = defaultFilterValues;

console.log($scope.filter);
console.log(defaultFilterValues);

If I set $scope.filter = { /* any data */ } both results are updated, how I can isolate defaultFilterValue to always have primary results?

2 Answers 2

2

Not 100% sure what you are wanting but you can use angular.copy() to make duplicate that isn't referenced to original object

Reference angular.copy() docs

$scope.filter = angular.copy(defaultFilterValues);

it will also clean out any internal hashkeys that angular adds to objects

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

1 Comment

then what exactly are you trying to do?
0

That is because all objects in JavaScript are copied by reference, you need

angular.extend($scope.filter, defaultFilterValues);

Comments

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.