gpt4 book ai didi

javascript - 通过使用字符串变量作为作用域名称来引用 $scope,例如$范围。[stringVar]

转载 作者:行者123 更新时间:2023-12-03 01:25:43 29 4
gpt4 key购买 nike

我需要更改 Controller 中范围的值,但特定范围的名称并未明确知道,并将作为变量发送到 Controller 函数。

具有显式作用域名称的示例函数是:

$scope.resetIsCustomer = function () {
$scope.cust = null;
};

我需要的示例是:

$scope.resetQuestionScope = function (scopeName) {
$scope.scopeName = null;
};

我不确定要搜索什么或可能的方法来做到这一点,类似的事情是在 Jquery 中通过 ID/Name 引用元素时,它们可以构造为 $('[name="' + this.id + 'pie"]') 或使用 typeof window[this.id]

检查是否存在

最佳答案

$scope.resetQuestionScope = function (scopeName) {
$scope[scopeName] = null;
};

与普通 js 相同,well Angular 就是普通 js :)

var obj = {};
obj.a = 2;
obj['a'] = 2;
var p = 'a';
obj[p] = 2;

关于javascript - 通过使用字符串变量作为作用域名称来引用 $scope,例如$范围。[stringVar],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51561100/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com