angularjs HTTP调用在angular.js中发生两次

0vvn1miw  于 2024-01-05  发布在  Angular
关注(0)|答案(2)|浏览(244)

在AngularJS中可能会多次调用重复的$http
抱歉,我没有50个声誉来评论同一篇文章。我们正在使用http服务调用API,所有服务都有通过cookiestore值传递的会话ID。API调用第一次不工作,因为会话ID在第一次调用期间为空,它只适用于第二次调用。
任何人都可以帮助我们这一点。我们有多个API的和所有发生两次,这实际上是增加了负载。是的,我们已经研究了它。

Router.js

  1. angular.module('adminsuite',['ngFileUpload','ui.router','ngCookies','angular-clipboard','ngAnimate', 'ngSanitize', 'ui.bootstrap','ngMessages']).constant("__env",env).config(function($stateProvider, $urlRouterProvider, $locationProvider) {
  2. $urlRouterProvider.otherwise('/');
  3. //$locationProvider.html5Mode(true);
  4. $stateProvider
  5. .state('login', {
  6. url: '/',
  7. views:{
  8. header:{
  9. templateUrl: '',
  10. controller: ''
  11. },
  12. pageContent:{
  13. templateUrl: 'Login/login3.html',
  14. controller: 'loginController'
  15. },
  16. footer:{
  17. templateUrl: 'common/footer3.html',
  18. controller: 'footerController'
  19. }
  20. }
  21. })
  22. // HOME STATES AND NESTED VIEWS ========================================
  23. .state('dashboard', {
  24. url: '/dashboard',
  25. views:{
  26. header:{
  27. templateUrl: 'common/header.html',
  28. controller: 'headerController'
  29. },
  30. pageContent:{
  31. templateUrl: 'dashboard/dashboard.html',
  32. controller: 'dashboardController'
  33. },
  34. footer:{
  35. templateUrl: 'common/innerFooter.html',
  36. controller: 'footerController'
  37. }
  38. }
  39. })
  40. //SURVEY STATES
  41. .state('survey', {
  42. url: '/survey',
  43. views:{
  44. header:{
  45. templateUrl: 'common/headerTool.html',
  46. controller: 'headerController'
  47. },
  48. pageContent:{
  49. templateUrl: 'survey/survey.html',
  50. controller: 'surveyController'
  51. },
  52. footer:{
  53. templateUrl: 'common/innerFooter.html',
  54. controller: ''
  55. }
  56. }
  57. });
  58. // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
  59. })

字符串

LoginAuthenticationService.js

  1. UserService.GetByUsername(requestData)
  2. .then(function (user) {
  3. console.log(user);
  4. if (user.SessionID) {
  5. sessionID = user.SessionID;
  6. userDetails = user.UserProfile;
  7. response = { success: true};
  8. } else {
  9. response = { success: false, message: 'Username or password is incorrect' };
  10. }
  11. callback(response);
  12. });


UserService.js

  1. function GetByUsername(user) {
  2. //console.log(__env.apiUrl);
  3. return $http.post(__env.apiUrl+'/UserAuthentication/login', user, {headers: { 'Content-Type': 'application/json'}}).then(handleSuccess, handleError('Error getting user by username'));
  4. }

Api.js

  1. $http.get(__env.apiUrl+'/UserSurvey/GetAllSurveys', {
  2. headers: { 'Content-Type': 'application/json','SessionID':$rootScope.token}
  3. })
  4. .then(function(response){
  5. console.log(response);
  6. return response.data;
  7. }, function(error){
  8. console.log("error");
  9. console.log(error);
  10. return error;
  11. });


任何帮助感激不尽。

yc0p9oo0

yc0p9oo01#

假设,你已经在你的应用程序中使用AngularUI状态定义声明了控制器,如下所示:

  1. $stateProvider
  2. .state('index',
  3. {
  4. url : '/',
  5. templateUrl : 'templates/home.html',
  6. controller : 'HomeController'
  7. })

字符串
然后,在home.html视图模板中,您还使用ng-controller指令声明控制器,如下所示:

  1. <div ng-controller="HomeController">


然后,你在没有意识到的情况下,两次附加了控制器(一次通过状态定义,两次通过ng-controller指令),这导致控制器中包含的代码也执行了两次。

展开查看全部
nszi6y05

nszi6y052#

您可能遇到了摘要周期问题。

当我的AJAX模块位于纯JavaScript中时,我的数据在第一个服务器查询上呈现。然而,当我将我的AJAX模块放在AngularJS模块中时,我必须在数据呈现之前查询服务器两次。注意,查询是从UI调用的。
在四处闲逛之后,我意识到数据确实在第一次查询时到达,并被分配给所有指定的对象和变量,但仅在AngularJS代码的范围内。数据已经异步到达,UI已经经历了一个摘要周期,没有理由再去一次,因为UI中没有任何变化。s a change in the UI Angular会更新所有内容,但当更改(到达数据)来自服务器时,Angular什么也不做。
随后,第二个相同的查询将强制一个摘要循环,并使用第一个查询中已经存在的数据更新所有绑定。因此,目标是从JavaScript强制一个摘要循环来更新你的UI绑定。我现在在回调函数的末尾强制一个摘要循环。为了强制一个摘要循环,在你的数据变量赋值完成之后放置Angular方法$scope.$apply([exp])。我在Angular文档中找到了有用的细节:https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply,在:http://jimhoskins.com/2012/12/17/angularjs-and-apply.html中也有一个很好的解释,以及在:How to call $scope.$apply() using "controller as" syntax中强制摘要循环时使用“控制器作为”语法的重要细节,希望这能解决双重HTTP调用的问题。

相关问题