我有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,
};
此处的目标是了解如何设置dynamic variables
。
我正在尝试在发布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
如何实现这一目标?
我现在可以接受任何建议。
任何提示/建议/帮助都将非常感谢!
答案 0 :(得分:2)
你可以这样做:
$scope.defined_vars[wifiIndex + '_devise_ssid']
答案 1 :(得分:1)
在所有示例中,您在本地定义变量(在if语句中),因此它们在if之外不可见。
您应该在函数的开头定义wifiIndex(例如var wifiIndex = ''
),以便它在整个函数中可见。
然后,您可以使用它将ssid和passphrase定义为:
ssid: $scope.defined_vars[wifiIndex + '_device_ssid']
passphrase: $scope.defined_vars[wifiIndex + '_device_passphrase']