在Angularjs应用页面上快速单击时无法加载

时间:2019-12-27 18:00:11

标签: angularjs frontend ngroute

仅供参考-我是Angular的新手,不是我的代码,主要来自我正在玩的Lynda教程。 我注意到,当我执行“分页”类型时,我正在显示data.json文件中的不同元素,如果我单击页面后退锚链接太快而无法显示下一个或上一个项目,则该页面不会加载。罪魁祸首始于细节控制器中此处/ anchor标签(details.html文件)的结果。我想知道它是否是async / await未被使用的问题。

<div class="container">
    <div class="row">
      <div class="col-12 mt-3">
          <div class="card">
            <div class="card-header d-flex align-items-start justify-content-between">
              <h1 class="card-title my-0">{{artists[whichItem].name}}</h1>
              <nav class="btn-group">
                <a class="btn btn-sm btn-secondary" 
                  href="#/details/{{prevItem}}">&lt;</a>
                <a class="btn btn-sm btn-secondary" 
                  href="#/">&bull;Home</a>
                <a class="btn btn-sm btn-secondary" 
                  href="#/details/{{nextItem}}">&gt;</a>
              </nav>
            </div>
            <div class="card-body"
              ng-model="artists">
              <h4 class="card-title text-dark mt-0">{{artists[whichItem].reknown}}</h4>
              <img class="float-left mr-2 rounded"
                ng-src="images/{{artists[whichItem].shortname}}_tn.jpg"
                alt="Photo of {{artists[whichItem].name}}">
              <div class="card-text text-secondary">{{artists[whichItem].bio}}</div>
            </div>
          </div>
      </div>
    </div>
  </div>

同时-通过在线搜索其他内容,我添加了

.otherwise({
  redirectTo: '/'
});

重定向,以便显示某些内容。如果有人可以帮助您解释造成该问题的原因以及解决方法,那就太好了。我在下面发布代码。我还添加了console.logs来帮助我在控制器中进行调试,但未成功。

我的小型控制器-(controllers.js):

var myControllers = angular.module('myControllers', []);

myControllers.controller('SearchController', function MyController($scope, $http) {
    $scope.sortArtistBy = 'name';
    $http.get('js/data.json').then(
      (response) => $scope.artists = response.data
    );
}); 

myControllers.controller('DetailsController', function MyController($scope, $http, $routeParams) {
  $http.get('js/data.json').then(
    function(response) {
      $scope.artists = response.data
      $scope.whichItem = $routeParams.itemId;

      if($routeParams.itemId > 0){
        $scope.prevItem = Number($routeParams.itemId) - 1;
        console.log("I am going to 18")
      } else {
        console.log("I am going to 20")
        $scope.prevItem = $scope.artists.length - 1;
      }

      if($routeParams.itemId < $scope.artists.length - 1){
        console.log("I am going to 25")
        $scope.nextItem = Number($routeParams.itemId) + 1;
      } else {
        console.log("I am going to 28")
        $scope.nextItem = 0;
      }
    }
  );
}); 

我的主要应用程序控制器(app.js):

var myApp = angular.module('myApp', [
  'ngRoute',
  'myControllers'
]);

myApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: 'js/partials/search.html',
      controller: 'SearchController'
    })
    .when('/details/:itemId', {
      templateUrl: 'js/partials/details.html',
      controller: 'DetailsController'
    })
    .otherwise({
      redirectTo: '/'
    });
}]);

我的(index.html)文件:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>AngularJS</title>
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="lib/bootstrap/bootstrap.min.css">
  <link rel="stylesheet" href="css/style.css">

  <script src="lib/angular/angular.min.js"></script>
  <script src="lib/angular/angular-route.min.js"></script>

  <script src="js/app.js"></script>
  <script src="js/controllers.js"></script>
</head>
<body class="bg-secondary">

<div ng-view></div>


<script src="lib/jquery/jquery.min.js"></script>
<script src="lib/bootstrap/popper.min.js"></script>
<script src="lib/bootstrap/bootstrap.min.js"></script>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

一种方法是将数据缓存在服务中:

app.service("dataService", function($http) {
    var cache;
    this.get = () => {
        cache = cache || $http.get('js/data.json');
        return cache;
    };
})

然后在控制器中:

app.controller('DetailsController', function MyController($scope, dataService, $routeParams) {
  dataService().then(
    function(response) {
      $scope.artists = response.data
      $scope.whichItem = $routeParams.itemId;
      //...
    }
  );
});

通过缓存$ http Promise,该应用程序避免了向服务器重复相同的请求。