在我的項目中,$ http調用每次都會被調用一次以上。檢查下面的代碼。
router.js
angular.module('adminsuite',['ngFileUpload','ui.router','ngCookies','angular-clipboard','ngAnimate', 'ngSanitize', 'ui.bootstrap','ngMessages']).constant("__env",env).config(function($stateProvider, $urlRouterProvider, $locationProvider) {
$urlRouterProvider.otherwise('/');
//$locationProvider.html5Mode(true);
$stateProvider
.state('login', {
url: '/',
views:{
header:{
templateUrl: '',
controller: ''
},
pageContent:{
templateUrl: 'Login/login3.html',
controller: 'loginController'
},
footer:{
templateUrl: 'common/footer3.html',
controller: 'footerController'
}
}
})
// HOME STATES AND NESTED VIEWS ========================================
.state('dashboard', {
url: '/dashboard',
views:{
header:{
templateUrl: 'common/header.html',
controller: 'headerController'
},
pageContent:{
templateUrl: 'dashboard/dashboard.html',
controller: 'dashboardController'
},
footer:{
templateUrl: 'common/innerFooter.html',
controller: 'footerController'
}
}
})
//SURVEY STATES
.state('survey', {
url: '/survey',
views:{
header:{
templateUrl: 'common/headerTool.html',
controller: 'headerController'
},
pageContent:{
templateUrl: 'survey/survey.html',
controller: 'surveyController'
},
footer:{
templateUrl: 'common/innerFooter.html',
controller: ''
}
}
}).state('survey.surveyList', {
url: '/:id',
templateUrl: 'survey/surveyList.html',
controller: ''
}).state('survey.surveyList.details', {
url: '',
templateUrl: 'survey/survey-details/summary.html',
controller: ''
}).state('survey.surveyList.questionare', {
url: '',
templateUrl: 'survey/questionare/questionare.html',
controller: 'questionareController'
}).state('survey.surveyList.sampleManagement', {
url: '',
templateUrl: 'survey/sample-management/sample-management.html',
controller: ''
}).state('survey.surveyList.quotaManagement', {
url: '',
templateUrl: 'survey/quota-management/quota-management.html',
controller: 'quotaController'
}).state('survey.surveyList.scheduling', {
url: '',
templateUrl: 'survey/scheduling/scheduling.html',
controller: ''
}).state('survey.surveyList.notification', {
url: '',
templateUrl: 'survey/notification/notification.html',
controller: ''
}).state('survey.surveyList.reports', {
url: '',
templateUrl: 'survey/reports/reports.html',
controller: ''
}).state('survey.surveyList.location', {
url: '',
templateUrl: 'survey/location/location.html',
controller: ''
});
// ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
})
loginAuthentication.js
UserService.GetByUsername(requestData)
.then(function (user) {
console.log(user);
if (user.SessionID) {
sessionID = user.SessionID;
userDetails = user.UserProfile;
response = { success: true};
} else {
response = { success: false, message: 'Username or password is incorrect' };
}
callback(response);
});
UserService.js
function GetByUsername(user) {
//console.log(__env.apiUrl);
return $http.post(__env.apiUrl+'/UserAuthentication/login', user, {headers: { 'Content-Type': 'application/json'}}).then(handleSuccess, handleError('Error getting user by username'));
}
當我CAL l這個函數在後端被多次調用。其他API也在發生同樣的事情。
我刪除了所有重複的控制器,但問題依然存在。
我們得到的一個特殊問題是在$ http調用的標題部分發送令牌標識,但是在後端發出的這個調用發生兩次,甚至多次。第一次,這個標題標記顯示空白,第二次或超過它給出結果的時間。請檢查下面的代碼。
API調用
$http.get(__env.apiUrl+'/UserSurvey/GetAllSurveys', {
headers: { 'Content-Type': 'application/json','SessionID':$rootScope.token}
})
.then(function(response){
console.log(response);
return response.data;
}, function(error){
console.log("error");
console.log(error);
return error;
});
顯示您的看法!你使用路由器嗎? – Sajeetharan
是的,我正在使用angular-ui-router – Kishan
那麼你不應該在視圖中包含ng-controller – Sajeetharan