我創建了一個使用本地存儲的簡單登錄應用程序。我有一個controller.js文件和一個service.js文件。如何阻止應用程序在angularJS中加載時運行服務Ionic
好的,在我的控制器中一切正常,我正確地調用服務。我正在設置一個localStorageService
,它包含一個默認的Prod url。如果用戶想要點擊另一個網址,我有一個Button-Bar
,他們可以選擇另一個網址並覆蓋默認網址。
當我點擊登錄按鈕運行服務時,它仍然使用默認的網址。有人可以向我解釋我做錯了什麼。以下是我的代碼。
Controller.js
app.controller('loginController', function($scope, $ionicModal, localStorageService, $location, loginDataStructure, loginAuthenticateService) {
//setting button count to zero on start
$scope.tapCount = 0;
//create url variable
var url;
$scope.baseUrl = function(){
$scope.tapCount++;
console.log('tapCount ='+ $scope.tapCount)
//setting url if user wants to change it.
$scope.setUrl = function(urlShort){
if(urlShort == 'PROD'){
url = 'https://www.prod.com/api';
}else if(urlShort == 'DEV'){
url = 'https://dev.dev.com/api';
}else if(urlShort == 'UAT'){
url = 'https://uat.uat.com/api';
}else if(urlShort == 'QA'){
url = 'https://qa.qa.com/api';
}else{
url = 'https://www.prod.com/api';
}
localStorageService.set('baseURL', url);
}
//login button
$scope.login = function(data) {
if (typeof(data) == 'undefined' || data === null) {
data = [];
}
$ionicLoading.show({
noBackdrop: true,
template: '<ion-spinner class="spinner-positive" icon="lines"/></ion-spinner><p class="item-icon-center">Loading</p>'
});
console.log('login data = ' + data.username + " " + data.password)
$scope.fromService = loginDataStructure.getUsers(data.username, data.password);
console.log('from service', $scope.fromService);
loginAuthenticateService.all($scope.fromService).then(function(JWTData) {
$scope.showAlert = function(errorText) {
var alertPopup = $ionicPopup.alert({
title: 'Error',
template: errorText
});
}
var jwt = JWTData;
var status;
console.log('data is ', JWTData)
status = JWTData.status;
if (status == '0') {
$scope.showAlert('Something went wrong, please try again laters');
$ionicLoading.hide();
} else if (status == '200') {
// now that the login is valid lets get the rest of the user data using the JWT
$ionicLoading.hide();
$ionicLoading.show({
noBackdrop: true,
template: '<ion-spinner class="spinner-positive" icon="lines"/></ion-spinner><p class="item-icon-center">Getting Data</p>'
});
}
}
service.js
app.service('loginDataStructure', function($http, $q, localStorageService) {
//setting the url to a variable
var url = localStorageService.get('baseURL');
console.log('url service =', localStorageService.get('url')
var headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
};
var params = "";
this.getUsers = function(username, password,url) {
// var url = ApiEndpoint.url ;
params = "UsernameOrEmail=" + username + "&Password=" + password;
console.log('params', params)
var req = {
method: 'POST',
url: url+ '/login',
headers: headers,
data: params
};
return req;
};
})
上述作品,但如果我從控制器來更改URL和命中登錄它那並不更改URL在控制器。