【问题标题】:AngularJS Checkbox work only false > true, for false > true case I must click Checkbox twiceAngularJS Checkbox 仅适用于 false > true,对于 false > true 的情况,我必须单击 Checkbox 两次
【发布时间】:2015-10-10 14:22:16
【问题描述】:

我在 Ionic & Cordova 环境中使用 AngularJS。我有一个切换样式的复选框 - 使用该复选框,用户可以激活或停用将通过 XHR-Request 保存在数据库中的选项。

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

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

那么:为什么必须先将复选框检查为真,然后才能将值检查回假?!

HTML/视图:

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

控制器,第 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;
                    };
                }
            });

控制器,第 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 以及其他一些没有解决的小变通方法。 感谢您的帮助。

【问题讨论】:

  • 谢谢,成功了! :)

标签: angularjs checkbox


【解决方案1】:

基于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;
                    };
                }

【讨论】:

  • 我已经更好地格式化了你的答案。 “谢谢”和其他噪音是不必要的。当您获得更多声誉时,您应该考虑将您认为有帮助的答案投票作为感谢。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2011-11-16
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多