AngularJS删除函数中的作用域变量

AngularJS remove scope variable in function

本文关键字:作用域 变量 删除 函数 AngularJS      更新时间:2023-09-26

如何删除范围变量,如果我传递它到函数没有从$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>

我想要一个完全独立的函数

您将能够从$scope删除属性的唯一方法是通过delete。如果您需要动态引用对象(即$scope)和属性,请尝试将密钥作为字符串传递,例如

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

$scope.example = function(scope, key) {
    delete scope[key];
};