1

I have 4 scope variables

$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

I know if I do it like this, I will get it to work.

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,
};

But

the goal here is to learn how to set the dynamic variables.


I am trying to set dynamically before making a 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);
    });


};

I kept getting

$scope.defined_vars.wifiIndex + '_device_ssid',

undefined_device_passphrase

How can I resolve this?

0

2 Answers 2

2

You can do this:

$scope.defined_vars[wifiIndex + '_devise_ssid']

Sign up to request clarification or add additional context in comments.

Comments

1

In all your examples, you define your variables locally (within a if statement), hence they are not visible outside your if.

You should define wifiIndex at the beginning of your function (var wifiIndex = '' for example) so that it will be visible within the entire function. Then, you can use it to define ssid and passphrase as :

ssid: $scope.defined_vars[wifiIndex + '_device_ssid']
passphrase: $scope.defined_vars[wifiIndex + '_device_passphrase']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.