1
我有一個切換,如果您將其值從false更改爲true,模式將彈出並要求您登錄。如果登錄成功,切換值應保持爲True。如果登錄過程出現問題(無效憑證),切換應該返回False。切換值沒有更新
app.controller('AppCtrl', function($scope, $localStorage, $http, $ionicModal, $timeout, $state, md5) {
$scope.invertToggle = function() {
$localStorage.value = !$localStorage.value;
$scope.value = $localStorage.value;
console.log("Toggle New Value = " + $scope.value);
}
$scope.setToggleTrue = function() {
$localStorage.value = true;
$scope.value = true;
console.log("Toggle New Value = " + $scope.value);
}
$scope.setToggleFalse = function() {
$localStorage.value = false;
$scope.value = false;
console.log("Toggle New Value = " + $scope.value);
}
$scope.change = function() {
$scope.invertToggle();
$ionicModal.fromTemplateUrl('templates/modal.html', function(modal) {
$scope.modal = modal;
}, {
animation: 'slide-in-up',
backdropClickToClose: false,
hardwareBackButtonClose: false,
focusFirstInput: true
});
if ($localStorage.value == true) {
console.log("Modal will pop up");
$scope.modal.show();
} else {
console.log("Unregister device");
}
}
$scope.submit = function() {
if (res.data != "Wrong Credentials") {
//We are not changing the toggle value here since login is ok
}
else{
$scope.setToggleFalse(); //login details were invalid, so we are setting toggle to false
}
})
.catch(function(error) {
console.error(error);
if (error.statusText == ""){
$scope.response = "Unexpected error. Make sure WiFi is on."
}
else {
$scope.response = "Error - "+error.status + " ("+error.statusText+")";
}
$scope.setToggleFalse(); //Unfortunatly here, an error occured so, we setting the Toggle back to False, just to be safe
})
.finally(function() {
console.log("Finally Function");
$timeout(function(){
$scope.modal.hide(); // we are hiding the modal, so the user returns to the previous page
$scope.$apply(function() { //I found somewhere $apply can do the trick but no luck
$scope.value = false; // Here I manually set the variable to false but again the toggle is still on on the page.
});
}, 3000);
});
};
});
下面是切換HTML:
<ion-toggle ng-model="value" ng-checked="value" ng-change="change()"toggle-class="toggle-royal">Register Device</ion-toggle>
我自動如何可以刷新頁面上的變量?我試圖通過設置緩存爲假,再次沒有運氣。我知道這些代碼很混亂,但是一旦所有事情都處於工作狀態,我將用henry hoover來清理污垢。 :)
現在即時通訊無法使用$ scope.modal.hide();我之前正面臨着這個問題,我通過刪除你提到的那一行來解決它。 – Bobys
我設法做到了我正在尋找的東西,但我沒有使用任何模態。我剛剛創建了一個新頁面,我將這些細節從一個頁面提取到另一個頁面 – Bobys