AngularJS Checkbox仅适用于false>是的,对于假>确实如此我必须单击Checkbox两次

时间:2015-07-21 08:21:48

标签: angularjs checkbox

我在AngonicJS中使用Ionic& amp;科尔多瓦环境。我有一个切换式的复选框 - 用户可以激活或取消激活一个选项,该选项将通过XHR-Request保存在数据库中。

案例1: 复选框已激活。我点击它 - 切换切换为false,但值仍保持为true。我必须再点击两次(切换回true然后再次返回false)>现在,Value变为false。

案例2: 复选框已停用。我点击它 - 切换切换为true,值切换为true,一切都很好。即使我切换回假,它也可以直接使用。

那么:为什么我的Checkbox必须检查为true,然后才能将值检回为false?!

HTML / VIEW:

<input type="checkbox" ng-model="setTrackOption110" ng-checked="isChecked110()" ng-change="stateChanged110(setTrackOption110)" />

CONTROLLER,第1部分 - 从数据库检查状态(无任何问题):

$http({
            method: 'GET',
            url: 'http://***&do=get&optid=110&userId=' + fdappAuth.fdappUserId + '&userPwHash=' + fdappAuth.fdappUserPw,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        })
            .success(function (data, status) {
                // Verarbeitet die Daten und setzt diese zur Anzeige in die scopes
                console.log("Optionsabfrage für Option 110 läuft");

                $scope.optentry = data[0];
                console.log("Rueckgabe JSON:");
                console.log($scope.optentry);

                $scope.optentry.opt_110 = data[0].opt_110;
                console.log("Optionswert:");
                console.log($scope.optentry.opt_110);

                if ($scope.optentry.opt_110 == 1) {
                    $scope.isChecked110 = function () {
                        // return true or false
                        return true;
                    };
                } else {
                    $scope.isChecked110 = function () {
                        // return true or false
                        return false;
                    };
                }
            });

CONTROLLER,第2部分 - 保护新值(这是问题所在):

// START OPTION 110 - TRACK & SHARE
        // Liest aus, ob der Wert geändert wird - wenn ja, wird die jeweilige Aktion ausgeführt
        $scope.stateChanged110 = function (checked) {
            console.log("STATUSÄNDERUNG || Neuer Status: checked: " + checked);
            if (checked == true) {

                // Start XHR Request
                $http({
                    method: 'GET',
                    url: 'http://*&do=set&optid=110&state=true&userId=' + fdappAuth.fdappUserId + '&userPwHash=' + fdappAuth.fdappUserPw,
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                    }
                })
                    .success(function (data, status) {
                        console.log("api call options erfolgreich");
                    });

            }
            if (checked == false) {
                // Start XHR Request
                $http({
                    method: 'GET',
                    url: 'http://*&do=set&optid=110&state=false&userId=' + fdappAuth.fdappUserId + '&userPwHash=' + fdappAuth.fdappUserPw,
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                    }
                })
                    .success(function (data, status) {
                        console.log("api call options erfolgreich");
                    });
            }
        };

我甚至测试过anguar.isDefined / isUndefined,或者使用ng-click代替ng-change和其他一些小的解决方法而无需解决。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

尝试初始化$scope.setTrackOption110 = false,主要是点/范围问题

答案 1 :(得分:0)

基于entry's answer

我在isChecked功能中添加了发布的行:

if ($scope.optentry.opt_110 == 1) {
                    $scope.isChecked110 = function () {
                        // return true or false
                        $scope.setTrackOption110 = true;
                        return true;
                    };
                } else {
                    $scope.isChecked110 = function () {
                        // return true or false
                        $scope.setTrackOption110 = false;
                        return false;
                    };
                }