gpt4 book ai didi

javascript - 构造动态变量 - AngularJS

转载 作者:行者123 更新时间:2023-11-30 11:35:36 25 4
gpt4 key购买 nike

我有 4 个作用域变量

$scope.defined_vars.p_24_device_ssid
$scope.defined_vars.p_50_device_ssid
$scope.defined_vars.g_24_device_ssid
$scope.defined_vars.g_50_device_ssid

我知道如果我这样做,我就会让它发挥作用。

if(section == 'private'){
if(freq == '2.4'){
var wifiIndex = 'p_24';
var ssid = $scope.defined_vars.p_24_device_ssid;
var passphrase = $scope.defined_vars.p_24_device_passphrase;
}else{
var wifiIndex = 'p_50';
var ssid = $scope.defined_vars.p_50_device_ssid;
var passphrase = $scope.defined_vars.p_50_device_passphrase;
}
}else{
if(freq == '2.4'){
var wifiIndex = 'g_24';
var ssid = $scope.defined_vars.g_24_device_ssid;
var passphrase = $scope.defined_vars.g_24_device_passphrase;
}else{
var wifiIndex = 'g_50';
var ssid = $scope.defined_vars.g_50_device_ssid;
var passphrase = $scope.defined_vars.g_50_device_passphrase;
}
}

var data = {
cpe_mac: $scope.cpe_mac,
vlan: section,
freq:freq,
ssid: ssid,
passphrase: passphrase,
};

但是

这里的目标是学习如何设置动态变量


我正在尝试在进行 POST 之前动态设置

$scope.updateWiFi = function(section,freq) {

if(section == 'private'){
if(freq == '2.4'){
var wifiIndex = 'p_24';
}else{
var wifiIndex = 'p_50';
}
}else{
if(freq == '2.4'){
var wifiIndex = 'g_24';
}else{
var wifiIndex = 'g_50';
}
}

var data = {
cpe_mac: $scope.cpe_mac,
vlan: section,
freq:freq,
ssid: $scope.defined_vars.wifiIndex + '_device_ssid',
passphrase: $scope.defined_vars.wifiIndex + '_device_passphrase',
};

console.log("PUT Data is " + angular.toJson(data));

$http({
method: 'PUT',
url: '/updateWiFi',
data: angular.toJson(data)
})

.then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log("%cError in updateWiFi()", "color: red;");
console.log(response.statusText);
});


};

我不断得到

$scope.defined_vars.wifiIndex + '_device_ssid',

undefined_device_passphrase

我该如何解决?

最佳答案

你可以这样做:

$scope.defined_vars[wifiIndex + '_devise_ssid']

关于javascript - 构造动态变量 - AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44571076/

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