我有4級範圍內的變量構造動態變量 - AngularJS
$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
一個如何去實現這個一種任務?
我接受任何建議在這一刻。
任何提示/建議/對此的幫助將非常感謝!