2

How to remove scope variable if I pass it into function without calling it from $scope?

// controller
$scope.user_id = 1;

$scope.example = function(uid) {
    // remove $scope.user_id without accessing it like a scope
    // smth like 

    uid = null; // won't work
};

// html
<div ng-click="example(user_id)">Click me!</div>

So I want to have completely isolated function

0

1 Answer 1

2

The only way you're going to be able to remove the property from $scope is via delete. If you need to dynamically reference the object (ie $scope) and the property, try passing the key as a string, eg

ng-click="example(this, 'user_id')"

and

$scope.example = function(scope, key) {
    delete scope[key];
};
Sign up to request clarification or add additional context in comments.

2 Comments

Okay I see. Thank you!
@NG I've added a more "isolated" example

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.