【发布时间】: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 以及其他一些没有解决的小变通方法。 感谢您的帮助。
【问题讨论】:
-
谢谢,成功了! :)