离子切换不影响正确的本地存储项

时间:2015-11-26 18:29:07

标签: javascript angularjs ionic-framework local-storage

离子切换出现问题,切换到哪里切换只会影响本地存储中的最后一个if语句。

这是切换列表...

$scope.settingsList = [

    { text: "GBP", checked: gbpON },
    { text: "USD", checked: usdON },
    { text: "EURO", checked: euroON },
    { text: "AUD", checked: audON },
    { text: "CAD", checked: cadON },
    { text: "YEN", checked: yenON }
];

这是HTML ...

<div class="list">  
    <ion-toggle ng-repeat="item in settingsList"
                ng-model="item.checked" 
                ng-checked="item.checked"
                ng-change="toggleONOFF({{item.text}})"
                toggle-class="toggle-royal">
      {{item.text}}
    </ion-toggle>
</div> 

这是单击切换时发生的情况......

$scope.toggleONOFF = function($var) {

    // GBP is toggled
    if ($var = "GBP") {   

        if (localStorage.getItem("gbpON") == "true") {  

            localStorage.setItem("gbpON", false);

        } else {

            localStorage.setItem("gbpON", true);    
        }
    };

    // USD is toggled
    if ($var = "USD") {   

        if (localStorage.getItem("usdON") == "true") {  

            localStorage.setItem("usdON", false);

        } else {

            localStorage.setItem("usdON", true);    
        }
    };

    // EURO is toggled
    if ($var = "EURO") {   

        if (localStorage.getItem("euroON") == "true") {  

            localStorage.setItem("euroON", false);

        } else {

            localStorage.setItem("euroON", true);    
        }
    }

    // AUD is toggled
    if ($var = "AUD") {   

        if (localStorage.getItem("audON") == "true") {  

            localStorage.setItem("audON", false);

        } else {

            localStorage.setItem("audON", true);    
        }
    }

    // CAD is toggled
    if ($var = "CAD") {   

        if (localStorage.getItem("cadON") == "true") {  

            localStorage.setItem("cadON", false);

        } else {

            localStorage.setItem("cadON", true);    
        }
    }

    // YEN is toggled
    if ($var = "YEN") {   

        if (localStorage.getItem("yenON") == "true") {  

            localStorage.setItem("yenON", false);

        } else {

            localStorage.setItem("yenON", true);    
        }
    }
}

我遇到的问题是切换到按下它只会改变本地存储中的日元而不是其链接的日元。例如toggleONOFF(GBP)

1 个答案:

答案 0 :(得分:0)

我认为在;语句中添加if-else是罪魁祸首,您还需要在if-else语句中使用raw之前初始化带有某些值或检查条件的localStorage。

if ($var = "GBP") {   
    if (localStorage.getItem("gbpON") == "true") {  
        localStorage.setItem("gbpON", false);
    } else {
        localStorage.setItem("gbpON", true);    
    }
};